Home  »  Articles  »  ASP.Net  »

Easy way To Send EMail with/without attachment Using ASP.Net & C#.Net or VB.Net

 Posted By : Manoranjan SahooPosted Date : 11/10/2010 07:25:07 PM Category : ASP.Net Points : 6 (Rs 2.00)

You can use the following code to send email.

using System.Net.Mail;
using System.Configuration;
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));
        }
    }
    message.Subject = subject;
    message.Body = messageBody;
    client.Host = "SMTPServerName";
    client.Port = 25;
    client.UseDefaultCredentials = true;
    client.Credentials = new System.Net.NetworkCredential("mail id", "password");
    client.Send(message); return 1;
}

You can send message with this code.

Send EMail with Attachment :

Follow the below code to send mail with attachment.

In 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;
}

In 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

Note : System.Net.Mail reads SMTP configuration data out of the standard .NET configuration system. So for ASP.NET applications you have to configure this in your application’s web.config file.  Here is an example of how to configure it:

<system.net>
    <mailSettings>
      <smtp deliveryMethod="Network">
        <network host="smtpserveraddress" port="25" defaultCredentials="true" />
      </smtp>
    </mailSettings>
</system.net>


Thank You.

Responses
No responses found for this thread. Be the first to respond this thread.
Post Reply
Login to post Response.