Tuesday, 7 March 2017

Send Mail With Attached File in C#.

public static void SendEmail()
{
try
{
string ToID = "ToMailId@demo.com", subject = "TestMail", message = "Test Mail Message.", FromId = "FromMailId@demo.com", username = "user", password = "pwd", smtp = "demo.mail.com";
int port = 25;

var loginInfo = new NetworkCredential(username, password);
var msg = new MailMessage(FromId,ToID);
var smtpClient = new SmtpClient(smtp, port);

msg.Subject = subject;
msg.Body = message;

System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("c:\\test.txt");
msg.Attachments.Add(attachment);

msg.IsBodyHtml = true;

smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = loginInfo;
smtpClient.Send(msg);
}
catch (Exception ex)
{
LogInfo.ExceptionLog(ex, "SendEmail", "SendEmail", true);
}
}

No comments:

Post a Comment