I have made a function for sending email and inside here smtp client object is created and set TLS for it
public Boolean SendMailNew(string filename, bool isAttach)
{
mailSent = false;
Boolean IsSuccess = false;
try
{
MailAddress senderAddress = new MailAddress(fromAddress, fromWhom);
MailMessage message = new MailMessage();
string messageString = "";
message.From = senderAddress;
string[] toAddressArray = ToAddressArray(toAddress);
foreach (string recAddress in toAddressArray)
{
message.To.Add(new MailAddress(recAddress, recAddress));
}
if (ccAddress != null && ccAddress.Trim().Length > 0)
{
string[] ccAd = ToAddressArray(ccAddress);
foreach (string ccAddStr in ccAd)
{
MailAddress ccAddr = new MailAddress(ccAddStr);
message.CC.Add(ccAddr);
}
}
if (bccAddress != null && bccAddress.Trim().Length > 0)
{
string[] bccAd = ToAddressArray(bccAddress);
foreach (string bccAddStr in bccAd)
{
MailAddress bcc = new MailAddress(bccAddStr);
message.Bcc.Add(bcc);
}
}
string path = ConfigurationSettings.AppSettings["ImagePath"].ToString();
LinkedResource lnkResource1 = new LinkedResource(path + "header1.jpg", MediaTypeNames.Image.Jpeg);
lnkResource1.ContentId = "headerPict1";
LinkedResource lnkResource2 = new LinkedResource(path + "dotted.jpg", MediaTypeNames.Image.Jpeg);
lnkResource2.ContentId = "dottedPict1";
LinkedResource lnkResource3 = new LinkedResource(path + "footer.jpg", MediaTypeNames.Image.Jpeg);
lnkResource3.ContentId = "footerPict1";
LinkedResource lnkResource4 = new LinkedResource(path + "f.jpg", MediaTypeNames.Image.Jpeg);
lnkResource4.ContentId = "fPict1";
LinkedResource lnkResource5 = new LinkedResource(path + "t.jpg", MediaTypeNames.Image.Jpeg);
lnkResource5.ContentId = "tPict1";
LinkedResource lnkResource6 = new LinkedResource(path + "yt.jpg", MediaTypeNames.Image.Jpeg);
lnkResource6.ContentId = "ytPict1";
LinkedResource lnkResource7 = new LinkedResource(path + "l.jpg", MediaTypeNames.Image.Jpeg);
lnkResource7.ContentId = "lPict1";
LinkedResource lnkResource8 = new LinkedResource(path + "robot.jpg", MediaTypeNames.Image.Jpeg);
lnkResource8.ContentId = "RobotPict1";
ContentType ct = new ContentType();
ct.MediaType = MediaTypeNames.Text.Html;
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(messageBody, ct);
htmlView.LinkedResources.Add(lnkResource1);
htmlView.LinkedResources.Add(lnkResource2);
htmlView.LinkedResources.Add(lnkResource3);
htmlView.LinkedResources.Add(lnkResource4);
htmlView.LinkedResources.Add(lnkResource5);
htmlView.LinkedResources.Add(lnkResource6);
htmlView.LinkedResources.Add(lnkResource7);
htmlView.LinkedResources.Add(lnkResource8);
message.AlternateViews.Add(htmlView);
messageString = messageBody;
message.Subject = subjectLine;
if (isAttach)
{
Attachment attach = new Attachment(filename);
// Add time stamp information for the file.
ContentDisposition disposition = attach.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(filename);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(filename);
disposition.ReadDate = System.IO.File.GetLastAccessTime(filename);
// Add the file attachment to this e-mail message.
message.Attachments.Add(attach);
}
message.IsBodyHtml = true;
message.Body = messageString;
//NetworkCredential loginInfo = new NetworkCredential(ConfigurationSettings.AppSettings["UserId"].ToString(), ConfigurationSettings.AppSettings["Password"].ToString());
//SmtpClient client = new SmtpClient("pod51014.outlook.com", 587);
//client.Port = Convert.ToInt32(ConfigurationSettings.AppSettings["SMTPport"]);
//client.EnableSsl = true;
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
//client.UseDefaultCredentials = false;
//client.DeliveryMethod = SmtpDeliveryMethod.Network;
//client.Credentials = loginInfo;
SmtpClient client = new SmtpClient(smtpClientString);
client.EnableSsl = true;
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
client.Send(message);
message.Attachments.Clear();
message.Dispose();
return (IsSuccess = true);
}
catch (Exception exc)
{
exc.ToString();
return IsSuccess;
//throw exc;
}
}
======================
Add the setting in webconfig file,Here from adress address must be same as user name
<system.net>
<mailSettings>
<smtp from="abc@test.net" deliveryMethod="Network">
<network userName="abc@test.net" password="5ama" host="po.outlook.com" port="587" />
</smtp>
</mailSettings>
</system.net>