.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Sending Emails from ASP.Net websites

Posted By:ASPEvil       Posted Date: June 15, 2014    Points: 200    Category: ASP.NET    URL: http://www.dotnetspark.com  

Sending Emails from ASP.Net Websites

Hello all,

Now a days, Internet become an essential part of our life. Even few years ago, many people would not know all about the internet, but now almost everybody is using internet. Thanks to the progress of science, technology and mankind.

Email is an essential part of internet which is used by all the internet users. So, there is a very need of this email technology for every website and as being ASP.Net developers, we also need to use this email technology in our ASP.Net websites. So, I am going to show an easiest and finest technique to send emails from ASP.Net websites, in this article.

To send emails, please follow the steps as described below:-

1) Please write a method as below--


Private Sub MessageBox(ByVal strMsg As String)

' generate a popup message using javascript alert function
' dumped into a label with the string argument passed in
' to supply the alert box text
Dim lbl As New Label
lbl.Text = ""

' add the label to the page to display the alert

End Sub


Note:- write this method in the code page of the ASP.Net webpage, where from you want to send emails.

2) Write the code as it is as below, in the code page too--

Dim mMailServer_me As String
Dim mTo_me As String
Dim mFrom_me As String
Dim mMsg_me As String
Dim mSubject_me As String
Dim mCC_me As String
Dim mPort_me As Integer

mTo_me = Trim(txt_email.Text)
mFrom_me = Trim("Don't-Reply@mywebsite.com")
mSubject_me = Trim("Welcome to www.mywebsite.com")
mMsg_me = Trim("You are welcomed to the www.mywebsite.com + " Don't Reply to this email as it is a system generated email.")
mMailServer_me = ConfigurationManager.AppSettings.Get("MyMailServer")
mPort_me = ConfigurationManager.AppSettings.Get("MyMailServerPort")
mCC_me = Trim(txt_email.Text)

Dim message_me As New MailMessage(mFrom_me, mTo_me, mSubject_me, mMsg_me)
If mCC_me <> "" Or mCC_me <> String.Empty Then
Dim strCC() As String = Split(mCC_me, ";")
Dim strThisCC As String
For Each strThisCC In strCC
End If

Dim mySmtpClient As New SmtpClient(mMailServer_me, mPort_me)
mySmtpClient.UseDefaultCredentials = True

MessageBox("Thanks for visiting our website!")

Catch ex As FormatException

MessageBox("Format Exception: " & ex.Message)

Catch ex As SmtpException

MessageBox("SMTP Exception: " & ex.Message)

Catch ex As Exception

MessageBox("General Exception: " & ex.Message)

End Try

3) Add few lines in the web.config as follows:--


    <add key="MyMailServer" value="localhost" />
    <add key="MyMailServerPort" value="25" />
Now run your website to send emails.

Some important points to be noted:-

a)These code work fine with many hosting providers and also don't work with a few hosting providers. Notice that, in the web.config file, I wrote a line as follows:--
<add key="MyMailServer" value="localhost" />

If your code is not working and throwing SMTP exception at runtime, then the [value="localhost"] of the above code section might be responsible for this. As this section tell the application to use the localhost of the server to send emails. But some web hosting provider just don't allow their localhost to send emails and instead they use a different special server to send emails. In this case, you please contact your hosting service provider to get the name of the server, so that you can use the name of that server in the place of "localhost" and if you are using godaddy.com as your hosting provider, then use "relay-hosting.secureserver.net" in place of "localhost".

b) If you are trying to check email sending from your local machine, then your IIS must be configured to do this email sending job, else you will get SMTP exception. If you are confused about the setting of the IIS, then just after writing these code, upload your webpage to the webserver of your webhosting service provider to see the effects.

Happy Website Development and Coding

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend