我正在尝试在系统处理完付款后向自己发送电子邮件通知。我使用以下代码:
public Exception SendEmail(string subject, string body)
try
{
var fromAddress = new MailAddress(Setting.Get("smtp_from"), Setting.Get("smtp_from_name"));
var toAddress = new MailAddress(Setting.Get("smtp_to"), Setting.Get("smtp_to"));
var smtp = new SmtpClient
{
Host = Setting.Get("smtp_host"),
Port = Int32.Parse(Setting.Get("smtp_port")),
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(fromAddress.Address, Setting.Get("smtp_password")),
Timeout = Int32.Parse(Setting.Get("smtp_timeout"))
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
return null; //All went well
}
catch (Exception ex)
{
return ex;
}
}
没有什么特别的,只有谷歌发现的基本代码。此代码可以完美地使用以下消息和主题:
Subject: [SUCCESS] Donation processed successfully
Message: This is a test.
但是,当消息更改为:
Donation received and processed successfully! No further action is required.
Donor: John Doe
Donation amount: 4,00 EUR
Email: fake@user.com
Mandrill API Response: Sent
我的收件箱中没有显示任何内容。它确实显示在发送电子邮件的帐户的已发送项目中。我检查了一切,垃圾邮件,捶打,但它没有到达。
之前有没有遇到过这个?有谁知道如何解决这个问题?
答案 0 :(得分:0)
这对我很有用:
public static void SendEmail(string emailbody)
{
MailMessage mailMessage = new MailMessage("incoming email address", "receiver email address");
mailMessage.Body = emailbody;
mailMessage.Subject = "Feedback";
SmtpClient smtpClient = new SmtpClient("relay-hosting.secureserver.net", 25);
smtpClient.EnableSsl = false;
smtpClient.Send(mailMessage);
}
希望这有帮助
答案 1 :(得分:0)
好的,我发现了问题。
看看这个:
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
using语句在调用.Send(message)方法之前处理MailMessage,并且由于某种原因,这不会产生异常。 facepalms