0

嗨,我正在尝试使用 gmail 凭据发送电子邮件并调用电子邮件模板,但它引发了发送邮件失败的异常

SmtpClient SmtpServer = new SmtpClient();
SmtpServer.Credentials = new System.Net.NetworkCredential("ambarishkesavarapu@gmail.com", "xxxxxx");
SmtpServer.Port = 587;
SmtpServer.Host = "smtp.gmail.com";
SmtpServer.EnableSsl = true;
message = new MailMessage();
message.Subject = "Visitor Arrived";
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = false;
message.Body = "EmailTemplate.html";
message.BodyEncoding = System.Text.Encoding.UTF8;
message.From = new MailAddress("ambarishkesavarapu@gmail.com");
message.To.Add(lblCPEmail.Text);
message.Priority = MailPriority.High;
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
SmtpServer.Send(message);

请帮帮我。

4

3 回答 3

0

您必须使用StreamReader从 html 文件中读取...并将MailMessage.BodyFormat属性设置为MailFormat.Html
Use This:

SmtpClient SmtpServer = new SmtpClient();
SmtpServer.Credentials = new System.Net.NetworkCredential("ambarishkesavarapu@gmail.com", "xxxxxx");
SmtpServer.Port = 587;
SmtpServer.Host = "smtp.gmail.com";
SmtpServer.EnableSsl = true;

using (StreamReader reader = File.OpenText(htmlFilePath)) // Path to your 
{                                                         // HTML file
    message = new MailMessage();
    message.Subject = "Visitor Arrived";
    message.SubjectEncoding = System.Text.Encoding.UTF8;
    message.IsBodyHtml = false;
    message.BodyEncoding = System.Text.Encoding.UTF8;
    message.From = new MailAddress("ambarishkesavarapu@gmail.com");
    message.To.Add(lblCPEmail.Text);
    message.Priority = MailPriority.High;

    message.Body = reader.ReadToEnd();  // Load the content from your file...
    //...
}
SmtpServer.Send(message);

部分答案来自发送带有 HTML 文件作为正文的电子邮件(C#)

于 2013-04-04T07:46:51.710 回答
0
            foreach (GnrDataMailInfo dmi in lstDMI)
            {
                MailMessage mail = new MailMessage();
                mail.From = "youremail";

                SmtpClient smtp = new SmtpClient();
                smtp.Port = 25;   // [1]  port
                smtp.EnableSsl = true;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network; // [2] Added this
                smtp.UseDefaultCredentials = false; // [3] Changed this
                smtp.Credentials = new NetworkCredential(mail.From.ToString(), "password");  // [4] Added this.
                smtp.EnableSsl = false;
                smtp.Timeout = 20000;
                smtp.Host = "yourhost";

                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                mail.BodyEncoding = System.Text.Encoding.UTF8;
                mail.HeadersEncoding = System.Text.Encoding.UTF8;
                mail.Subject = dmi.Subject;
                mail.To.Add(dmi.To);
                mail.IsBodyHtml = true;
                mail.Body = dmi.Body;

                smtp.Send(mail);
            }
于 2016-11-03T07:48:26.450 回答
0

改用端口 465

端口 465 用于 smtps SSL 加密在任何 SMTP 级别通信之前自动启动。

端口 587 用于 msa 它几乎就像标准 SMTP 端口。MSA 应该在身份验证后接受电子邮件(例如,在 SMTP AUTH 之后)。当 DUL 范围的网络管理员可以阻止到 SMTP 端口(端口 25)的传出连接时,它有助于阻止传出的垃圾邮件。

于 2016-11-03T08:00:27.343 回答