Disable Windows Task Manager (ALT+CTRL+DEL)
Jul 29

Today I came across someone who was having trouble sending email from their ASP.net webpage, so I decided to create a little example of how to do so.

Step 1:
Before you can use my coding you must add my Send_Mail() method to your page’s behind the scenes class. To do this, right click on your .aspx page in your Solution Explorer, and select “View Code”. Once in this window, scroll to the very body, and right above “End Class“, paste the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
''' <summary>
''' This will allow you to send email in one simple sub call
''' </summary>
'''<param name="smtp_username">The login for your SMTP server. Ussualy same as your pop3 login</param>
'''<param name="smtp_password">The password for your SMTP server, ussualy same as your pop3 password</param>
'''<param name="smtp_server">The mail server to send to. Ussualy mail.yourdomain.com</param>
'''<param name="smtp_port">The port for your smtp server. More than likely always 25</param>
'''<param name="mail_from">Who to send mail from. For alot of servers (including smarter mail) this must be same as smtp_username</param>
'''<param name="mail_to">Who we are sending the mail to</param>
'''<param name="mail_subject">What your mail subject will be</param>
'''<param name="mail_body">What your mail body will be, HTML enabled</param>
''' <remarks>created by www.CoderGuru.com</remarks>
Public Sub Send_Mail(ByVal smtp_username As String, ByVal smtp_password As String, ByVal smtp_server As String, ByVal smtp_port As String, ByVal mail_from As String, ByVal mail_to As String, ByVal mail_subject As String, ByVal mail_body As String)
 
  'Dont edit anything below here unless you want BCC, simply uncomment my line
  Dim mail As New System.Net.Mail.MailMessage(mail_from, mail_to)
  mail.Subject = mail_subject
  mail.IsBodyHtml = True
  mail.Body = mail_body
  'mail.Bcc.Add("you_get_copy_too@foo.bar") 'Uncomment to add BCC's to this message
 
  Dim smtp As New System.Net.Mail.SmtpClient(smtp_server, smtp_port)
  Dim nc As New System.Net.NetworkCredential(smtp_username, smtp_password)
  smtp.Credentials = nc
  smtp.Send(mail)
 
End Sub

Step 2:
Once you have the method loaded, in your send button paste the following and fill in the fields needed:

1
2
3
4
5
6
7
8
9
10
11
12
  'Below holds your SMTP login information
  Dim smtp_username As String = "username here"
  Dim smtp_password As String = "password here"
  Dim smtp_server As String = "mail.yourdomain.com"
  Dim smtp_port As Integer = 25'Will more than likely never change
 
  Dim mail_from As String = smtp_username' For me my login is my email so simply reference that string
  Dim mail_to As String = "some_Email@somedomain.com"
  Dim mail_subject As String = "Hello World!"
  Dim mail_body As String = "look mom, my email holds <b>html</b> =0"
 
  Send_Mail(smtp_password, smtp_password, smtp_server, smtp_port, mail_from, mail_to, mail_subject, mail_body)

Check back soon for a working example in both VB.net and C#

Share and Enjoy:
  • E-mail this story to a friend!
  • Digg
  • Facebook
  • Twitthis
  • Twitter
  • del.icio.us
  • StumbleUpon
  • Google Bookmarks
  • Technorati
  • blogmarks
  • Sphinn
  • Mixx
  • FriendFeed
  • Print this article!
  • Turn this article into a PDF!
  • RSS
  • NuJIJ
  • ppnow

2 Responses to “Sending Email from ASP.net”

  1. Jeremy says:

    How do you add line breaks to the mail_body?

    I’ve tried useing vbCr with no luck…

    Regards,
    Jeremy

    • admin says:

      Inside my Send_Mail function, you will see I have “mail.IsBodyHtml = True” which sets the email type to HTML. Because of this, if you want a new line, you have to add the HTML line break tag of “<br />”. Hope that helps.

Leave a Reply

preload preload preload