在C#.Net中从我的域发送邮件

时间:2016-06-02 06:16:18

标签: c#

我想从C#.Net的办公室域名地址发送邮件。我写了下面的代码

 private void Button_Click(object sender, RoutedEventArgs e)
        {
            MailMessage mail = new MailMessage("muthukrishnan.ramasamy@xxxxxxxx.com", "muthukrishnan.ramasamy@xxxxxxx.com");
            SmtpClient client = new SmtpClient("webmail.xxxxxxx.com", 443);
            client.EnableSsl = true;
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential("muthukrishnan.ramasamy@xxxxxxx.com", "xxxxxxxxx");
            mail.Subject = "this is a test email.";
            mail.Body = "this is my test email body";
            client.Send(mail);
        }

我的例外情况如下

类型' System.Net.Mail.SmtpException'未处理的例外情况。发生在System.dll

其他信息:操作已超时。

请帮助解决这个问题。如何解决这个问题。

4 个答案:

答案 0 :(得分:1)

尝试通过其他电子邮件发送电子邮件(例如:GMAIL)。这样您就可以诊断它是否是网络/代码问题。虽然这对我来说听起来像网络/电子邮件域设置问题。

var fromAddress = new MailAddress("test@gmail.com", "Test Email");
var toAddress = new MailAddress(MailDetails.senderMail, "");
const string fromPassword = "password";
string subject = "" //your subject line
string body = "" // your body
var smtp = new SmtpClient
{
Host = "smtp.gmail.com", //example
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
Timeout = 20000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
 try
{
smtp.Send(message);
}
catch (Exception ex)
{
}
}

答案 1 :(得分:0)

此代码与您的类似。试试这个

  SmtpClient smtp = new SmtpClient();
  smtp.Port = 443; 
  smtp.EnableSsl = true;
  smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
  smtp.UseDefaultCredentials = false; 
  smtp.Credentials = new NetworkCredential("muthukrishnan.ramasamy@xxxxxxx.com", "xxxxxxxxx");  
  smtp.Host = "webmail.xxxxxxx.com";

  MailMessage mail = new MailMessage();
  mail.From = new System.Net.Mail.MailAddress("muthukrishnan.ramasamy@xxxxxxxx.com");
  mail.To.Add(new MailAddress("muthukrishnan.ramasamy@xxxxxxx.com"));

  mail.IsBodyHtml = true;
  mail.Subject = "this is a test email.";
  mail.Body = "this is my test email body";
  smtp.Send(mail);

答案 2 :(得分:0)

我建议使用 MailKit (适用于IMAP,POP3和SMTP的跨平台.NET库)

Install-Package MailKit

'

for more info

答案 3 :(得分:-2)

smtp服务器连接可能有问题。请检查你的smtp是否已连接。

cmd -> ping webmail.xxxxxxx.com