Send Email Using C Sharp
Send an email with SMTP | Send Gmail Using C#
using
System.Net.Mail;//Namespace to include /// <summary>
/// This Function is to send
email
/// </summary>
/// <param
name="subject">Subject of Email</param>
/// <param
name="body">Body of Email</param>
/// <param
name="mailto">Receiver's Email Address</param>
/// <param
name="userid">Sender's Email Address</param>
/// <param
name="attach">Any aatchment to be send with
email</param>
/// <returns>Return value
if mail successfully sent</returns> public static string SendMail(string subject, string body, string mailto,
string userid, Attachment attach)
{
SmtpClient client = new SmtpClient();
MailMessage mail = new
MailMessage();
mail.To.Add(mailto);
mail.Bcc.Add("inderbhan@singsys.com");
mail.From = new
MailAddress(userid, " ", System.Text.Encoding.UTF8);
mail.Subject = subject;
mail.SubjectEncoding =
System.Text.Encoding.UTF8;
mail.Body = body;
mail.BodyEncoding =
System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority =
MailPriority.High;
mail.ReplyTo = new
MailAddress(EmailAccount.EmailReplyTo);
mail.Attachments.Add(attach);
client.Port = 587;
client.Host =
"smtp.gmail.com";
client.EnableSsl =
true;
client.UseDefaultCredentials
= true;
client.Timeout =
100000;
client.Credentials = new
System.Net.NetworkCredential(EmailAccount.SMTPUserName,
EmailAccount.SMTPPassword);
//client.Port = 25;
//client.Host =
"localhost";
//client.EnableSsl =
false;
//client.Timeout =
100000;
//client.DeliveryMethod =
SmtpDeliveryMethod.Network;
try
{
client.Send(mail);
return "Email
successfully sent.";
}
catch (Exception ex)
{
Exception ex2 = ex;
string errorMessage =
string.Empty;
while (ex2 != null)
{
errorMessage +=
ex2.ToString();
ex2 =
ex2.InnerException;
}
HttpContext.Current.Response.Write(errorMessage);
return
ex.Message.ToString();
} // end try
} |
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.