Send Mail with Attachment using ASP.Net and C#.net or VB.Net


This Example is described about how to send mail with attachment using ASP.Net and C#.Net or VB.Net

Here I am giving you the code to send mail with attachment by using ASP.Net and C#.Net or VB.Net

Use Following code to send mail with attachment.



Follow the below code to send mail with attachment.

Using C#.Net :



public int SendMessage(string subject, string messageBody, string fromAddress, string toAddress)
{
MailMessage message = new MailMessage();
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
message.From = new MailAddress(fromAddress.ToString());
if (toAddress.Trim().Length > 0)
{
foreach (string addr in toAddress.Split(';'))//you can use any delimiter
{
message.To.Add(new MailAddress(addr));
}
}
Attachment attachFile = new Attachment("D:\\a.xls");//you can also get the file name with using fileupload control
message.Attachments.Add(attachFile);
message.Subject = subject;
message.Body = messageBody;
client.Host = "smtp address";
client.Port = 587;//put your smtp port number
client.UseDefaultCredentials = true;
client.Credentials = new System.Net.NetworkCredential("mail id", "password");
client.Send(message);
return 1;
}


Using VB.Net :



Public Function SendMessage(ByVal subject As String, ByVal messageBody As String, ByVal fromAddress As String, ByVal toAddress As String) As Integer
Dim message As MailMessage = New MailMessage()
message.IsBodyHtml = True
Dim client As SmtpClient = New SmtpClient()
message.From = New MailAddress(fromAddress.ToString())
message.To.Add(New MailAddress(toAddress.ToString()))
Dim attachFile As Attachment = New Attachment("D:\\a.xls") 'specify your file location
message.Attachments.Add(attachFile)
message.Subject = subject
message.Body = messageBody
client.Host = "smtp address" ' put your smtp address
client.Port = 587 ' put your port number for smtp
client.UseDefaultCredentials = True
client.Credentials = New System.Net.NetworkCredential("smtp login mail id", "smtp login password")
client.Send(message)
Return 1
End Function

Thank You.

Reference: http://msahoo.wordpress.com/2009/06/03/easy-way-to-send-mail-through-asp-net-c-net/


Article by Manoranjan Sahoo
If it helps you then Rate this. Best Regards, Manoranjan Sahoo http://www.dotnetsquare.com Blog: http://blog.msahoo.net

Follow Manoranjan Sahoo or read 63 articles authored by Manoranjan Sahoo

Related Articles

Read Mails from ASP.NET

This article Illustrate How to read mails from ASP.Net. Using POP commands you can access you email inbox from ASP.Net. Basic POP commands are USER, PASS, LIST, QUIT, RETR.

More articles: Email using c# Email ASP.Net Sending Email

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: