SmtpClient obj = new SmtpClient("smtp.gmail.com",587);
//587 is the port number supplied by gmail team. we can use 456 also
obj.EnableSsl = true;
obj.Credentials=new System.Net.NetworkCredential("username@gmail.com", "password");
MailMessage Mailmsg = new MailMessage();
Mailmsg.To.Clear();
Mailmsg.From = new MailAddress("username@gmail.com");
Mailmsg.Subject = "Subject";
Mailmsg.Body ="Body";
//adding attachments
Attachment at = new Attachment(Server.MapPath("~/style.css"));
Mailmsg.Attachments.Add(at);
Mailmsg.Priority = MailPriority.High;
Mailmsg.IsBodyHtml = true;
Mailmsg.To.Add(new MailAddress("mailaddress", "name"));
Mailmsg.BodyEncoding = System.Text.Encoding.Default;
try
{
obj.Send(Mailmsg);
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
-------------------example how to use above code--------------
SmtpClient obj = new SmtpClient("smtp.gmail.com", 587);
//587 is the port number supplied by gmail team. we can use 456 also
obj.EnableSsl = true;
obj.Credentials = new System.Net.NetworkCredential("admg.fms@gmail.com","quantum123");
MailMessage Mailmsg = new MailMessage();
Mailmsg.To.Clear();
Mailmsg.From = new MailAddress("admg.fms@gmail.com");
Mailmsg.Subject = "Application rejected";
Mailmsg.Body = "Body";
//adding attachments
Attachment at = new Attachment(Server.MapPath("~/photo.jpg"));
Mailmsg.Attachments.Add(at);
Mailmsg.Priority = MailPriority.High;
Mailmsg.IsBodyHtml = true;
Mailmsg.To.Add(new MailAddress("kishoreswrn@gmail.com", "kishore"));
Mailmsg.BodyEncoding = System.Text.Encoding.Default;
try
{
obj.Send(Mailmsg);
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
Response.Write("Mail Sent Successfully");
}
No comments:
Post a Comment