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.NetUse 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/