如何使用office365帐户从Azure网站发送带附件的电子邮件

时间:2015-05-22 03:51:07

标签: c# azure office365 azure-web-sites smtpclient

我有Office 365帐户,使用它我想在我的Web应用程序中发送带有附件的电子邮件,该应用程序部署在Azure网站中。

1 个答案:

答案 0 :(得分:0)

这是执行此功能的功能

 public void SendEmailWithAttachment()
        {
            var objMailMessage = new MailMessage("EnterFromEmailAddress", "ReceiverEmailAddress")
            {
                Subject = "EnterEmailSubject",
                IsBodyHtml = true,
                Body = "EnterEmailBody"
            };
            //You can add more attachments
            objMailMessage.Attachments.Add(new Attachment("EnterAttachmentLocationPath")); 
            var smtp = new SmtpClient("smtp.office365.com");
            var login = new NetworkCredential("SenderEmail","SenderEmailPAssword");
            smtp.Credentials = login;
            smtp.Port =  587;
            smtp.EnableSsl = true; 
            smtp.Send(objMailMessage); 
        } 

最好将web.config中的凭据或凭据保留为应用程序设置。