您好我正在尝试使用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);
请帮帮我。
答案 0 :(得分:0)
您必须使用StreamReader
来读取html文件....并将MailMessage.BodyFormat
属性设置为MailFormat.Html
使用此:
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);
答案 1 :(得分: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);
}
答案 2 :(得分:0)
使用端口465代替
端口465用于smtps SSL加密在任何SMTP级别通信之前自动启动。
端口587用于msa 它几乎像标准的SMTP端口。 MSA应在验证后接受电子邮件(例如在SMTP AUTH之后)。当DUL范围的网络管理员可以阻止到SMTP端口(端口25)的传出连接时,它有助于阻止外发垃圾邮件。