正如标题所示,我正在尝试从我的C#应用程序发送电子邮件,我遇到了一些麻烦。
我编写了下面的函数,以便更容易从我的应用程序发送邮件,但我相信某处肯定存在问题而我无法看到它。也许这就是“看不见森林的树木”的情景。
当我尝试通过SMTP发送电子邮件时出现问题。页面似乎超时,没有任何错误消息... LocalPickup工作,指定一个拾取目录,但在这种情况下,我需要使用SMTP。
在这种情况下,我的网站位于我的家庭开发服务器(运行Windows Server 2003)上,我的SMTP服务器是一个运行带有Qmail的CentOS Linux的远程专用盒。
我已经包含了我编写的功能,只是为了回答任何问题。是的,这台服务器上的SMTP端口肯定是26;)
/// <summary>
/// Sends an email
/// </summary>
/// <param name="To">Addresses to send the email to, comma seperated</param>
/// <param name="subject">Subject of the email</param>
/// <param name="emailBody">Content of the email</param>
/// <param name="cc">CC addresses, comma seperated [Optional]</param>
/// <param name="Bcc">BCC addresses, comma seperated [Optional]</param>
/// <param name="client">How to send mail, choices: iis, network, directory. [Optional] Defaults to iis</param>
/// <returns></returns>
public bool sendMail(string To, string subject, string emailBody, string from, string cc = "", string Bcc = "", string client = "network", bool html = true)
{
// Create a mailMessage object
MailMessage objEmail = new MailMessage();
objEmail.From = new MailAddress(from);
// Split email addresses by comma
string[] emailTo = To.Split(',');
foreach (string address in emailTo)
{
// Add these to the "To" address
objEmail.To.Add(address);
}
// Check for CC addresses
if (cc != "")
{
string[] emailCC = cc.Split(',');
foreach (string addressCC in emailCC)
{
objEmail.CC.Add(addressCC);
}
}
// Check for Bcc addresses
if (Bcc != "")
{
string[] emailBCC = Bcc.Split(',');
foreach (string addressBCC in emailBCC)
{
objEmail.Bcc.Add(addressBCC);
}
}
// Set the subject.
objEmail.Subject = subject;
// Set the email body
objEmail.Body = emailBody;
// Set up the SMTP client
SmtpClient server = new SmtpClient();
switch (client)
{
case "iis":
server.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
break;
case "network":
server.DeliveryMethod = SmtpDeliveryMethod.Network;
NetworkCredential credentials = new NetworkCredential("SmtpUserName", "SmtpPassword");
server.Host = "SmtpHost";
server.Port = 26;
server.Credentials = credentials;
break;
case "directory":
server.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
server.PickupDirectoryLocation = "c:\\mailpickup";
break;
default:
throw new Exception("Invalid delivery method specified, cannot continue!");
}
if (html)
{
// As the email is HTML, we need to strip out all tags for the plaintext version of the email.
string s = emailBody;
s = Regex.Replace(s, "<.*?>", string.Empty);
s = Regex.Replace(s, "<script.*?</script>", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);
AlternateView plainText = AlternateView.CreateAlternateViewFromString(s, null, MediaTypeNames.Text.Plain);
objEmail.AlternateViews.Add(plainText);
AlternateView rich = AlternateView.CreateAlternateViewFromString(emailBody, null, MediaTypeNames.Text.Html);
objEmail.AlternateViews.Add(rich);
}
try
{
server.Send(objEmail);
return true;
}
catch(Exception ex)
{
throw new Exception(ex.ToString());
}
正如我所说,页面在大约60秒后完全挂起,没有任何错误信息可供查看。
提前致谢,
戴夫
添加: - 这就是我调用sendMail()
的方式webMail sendConfirmation = new webMail();
fileSystem fs = new fileSystem();
siteSettings setting = new siteSettings();
string mailBody = fs.file_get_contents("http://myurl.com/mymessage.html");
// Run any replaces.
mailBody = mailBody.Replace("{EMAIL_TITLE}", "Your account requires confirmation");
mailBody = mailBody.Replace("{U_FNAME}", u_forename);
mailBody = mailBody.Replace("{REG_URL_STRING}", setting.confirmUrl);
sendConfirmation.sendMail(u_emailAddress, "Your account requires confirmation", mailBody, setting.siteEmail);
答案 0 :(得分:3)
您可以尝试检查错误:
SmtpClient smtp = new SmtpClient();
smtp.SendCompleted += new SendCompletedEventHandler(smtp_SendCompleted);
smtp.Send(msgMail);
void smtp_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
if (e.Cancelled == true || e.Error != null)
{
throw new Exception(e.Cancelled ? "EMail sedning was canceled." : "Error: " + e.Error.ToString());
}
答案 1 :(得分:1)
无法为rcpt域找到有效的MX通常意味着无法找到有效的电子邮件地址或电子邮件域来将电子邮件中继到:我会查看正在拆分的“到”电子邮件地址数组,以确保每个一个有效/来自有效域。可能会向每个“到”电子邮件地址发送一个测试,以便您可以验证这是否是smtp服务器问题。
另一种可能性是localhost / iis权限,用于中继到另一个smtp服务器“??”
我的单地址测试代码:
public void Send(string from, string to,string smtpServer, int smtpPort,string username, string password)
{
try
{
using (MailMessage mm = new MailMessage())
{
SmtpClient sc = new SmtpClient();
mm.From = new MailAddress(from, "Test");
mm.To.Add(new MailAddress(to));
mm.IsBodyHtml = true;
mm.Subject = "Test Message";
mm.Body = "This is a test email message from csharp";
mm.BodyEncoding = System.Text.Encoding.UTF8;
mm.SubjectEncoding = System.Text.Encoding.UTF8;
NetworkCredential su = new NetworkCredential(username, password);
sc.Host = smtpServer;
sc.Port = smtpPort;
sc.Credentials = su;
sc.Send(mm);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
答案 2 :(得分:1)
对于我的公司,我们遇到此问题的原因是因为发送电子邮件的服务器或计算机未包含在电子邮件服务器的白名单中。一旦机器的IP地址被列为白色,它就开始工作了。出于同样的原因,您可能需要检查电子邮件服务器黑名单。
答案 3 :(得分:0)
有时以这种方式发送的邮件往往会完成垃圾邮件,特别是如果来自地址是幻想的话。尝试使用telnet的原始ftp作为P.Brian.Macket告诉你是一个好主意。
答案 4 :(得分:0)
您的默认客户端我们的网络,但是当您定义网络客户端时,您使用的是一些默认代码(用户名,密码和主机)。
case "network":
server.DeliveryMethod = SmtpDeliveryMethod.Network;
NetworkCredential credentials = new NetworkCredential("SmtpUserName", "SmtpPassword");
server.Host = "SmtpHost";
server.Port = 26;
server.Credentials = credentials;
break;
我真的认为使用示例代码而忘记指定自定义配置。
我为我的英语道歉。
答案 5 :(得分:-2)
“要”是c#中的保留字吗?尝试更改它,看看你得到了什么。