For a developer, e-mail is an effective way to allow an application to send files or reports to users and to notify users of problems or events.Here i am going to explain how can send mail through VB.net or C#.
To send an email you need two objects:
1.MailMessage object: with this object you can create your message that you want to send.
2.SmtpClient object: with this object you can send your MailMessage to the recipients.
Before you create a function that send mail ,you need to import System.Net.Mail :
[VB.Net]
Imports System.Net.Mail
[C#]
using System.Net.Mail;
Now your code is ready for creating SendMail function
[VB.Net]
Private Sub SendMail() Dim Message As MailMessage = New MailMessage() Message.From = New MailAddress("Arman@codingtips.net", "Arman") Message.To.Add(New MailAddress("Mehran@codingtips.net", "Mehran")) Message.To.Add(New MailAddress("Mike@codingtips.net", "Mike")) Message.To.Add(New MailAddress("JM@codingtips.net", "JM")) Message.Subject = "Quarterlydata report." Message.Body = "See the attached spreadsheet." Message.Attachments.Add(New Attachment("C:\Test.txt")) Dim sc As SmtpClient = New SmtpClient("smtp.gmail.com") sc.Port = 587 sc.EnableSsl = True sc.Credentials = New NetworkCredential("Username", "Password") sc.Send(Message) End Sub
[C#]
private void SendMail() { MailMessage Message = new MailMessage(); Message.From = new MailAddress("Arman@codingtips.net", "Arman"); Message.To.Add(new MailAddress("Mehran@codingtips.net", "Mehran")); Message.To.Add(new MailAddress("Mike@codingtips.net", "Mike")); Message.To.Add(new MailAddress("JM@codingtips.net", "JM")); Message.Subject = "Quarterlydata report."; Message.Body = "See the attached spreadsheet."; Message.Attachments.Add(new Attachment("C:\\Test.txt")); SmtpClient sc = new SmtpClient("smtp.gmail.com"); sc.Port = 587; sc.EnableSsl = true; sc.Credentials = new NetworkCredential("Username", "Password"); sc.Send(Message); }
Caution:
SmtpClient.port: it depends on the server that you want to use it to send mail.
SmtpClient .Host: it depends on the server that you want to use it to send mail. (here I used “smtp.gmail.com”)
SmtpClient.EnableSsl: If your server is using the SSL you can user this option.
Thanks for some other great article. The place else may anybody get that type of info in such an ideal means of writing? I’ve a presentation next week, and I am at the look for such information.