2013-05-05 66 views
0

发送邮件预期 但现在我的要求是从Windows Mail中server.whenever发送邮件我尝试发送邮件它显示给我错误发送邮件失败。如何在asp.net从我试图从Gmail和其他共享发送邮件托管邮件服务器和其作品的Windows Mail服务器

SmtpClient client = new SmtpClient(); 
client.DeliveryMethod = SmtpDeliveryMethod.Network; 

client.Host = "us2.webmail.mailhostbox.com"; 
string fromaddress = "[email protected]"; 
client.Port = 25; 
//client.Host = "mail.csisite.com"; 
// setup Smtp authentication 
System.Net.NetworkCredential credentials = 
    new System.Net.NetworkCredential("[email protected]", "password"); 
//client.UseDefaultCredentials = true; 
client.Credentials = credentials; 
client.EnableSsl = false; 
try 
{ 

//mail For Customer 
MailMessage msgcustomer = new MailMessage(); 
msgcustomer.From = new MailAddress(fromaddress); 
msgcustomer.To.Add(new MailAddress(EmailID)); 

msgcustomer.Subject = "Thank you for writing to us" ; 
msgcustomer.IsBodyHtml = true; 
msgcustomer.Body = "Test Mail"; 
client.Send(msgcustomer) 

回答

0

使用System.Net.Mail

MailMessage msgMail = new MailMessage(); 

    MailMessage myMessage = new MailMessage(); 
    myMessage.From = new MailAddress("sender's email","sender`s name and surname"); 
    myMessage.To.Add("recipient's email"); 
    myMessage.Subject = "Subject"; 
    myMessage.IsBodyHtml = true; 

    myMessage.Body = "Message Body"; 


    SmtpClient mySmtpClient = new SmtpClient(); 
    System.Net.NetworkCredential myCredential = new System.Net.NetworkCredential("email", "password"); 
    mySmtpClient.Host = "your smtp host address"; 
    mySmtpClient.UseDefaultCredentials = false; 
    mySmtpClient.Credentials = myCredential; 
    mySmtpClient.ServicePoint.MaxIdleTime = 1; 

    mySmtpClient.Send(myMessage); 
    myMessage.Dispose(); 

更新的用户越来越以下错误

{ “A插座操作尝试无法访问网络208.91.199.230:25”}和错误代码是10051

的错误10051是当目标网络或主机服务器无法到达出现一个套接字错误。错误可能是由于路由器的错误配置,Internet断开连接或防火墙阻止操作导致的。大多在试图生成Internet控制消息协议(ICMP),并与错误配置简单邮件传输协议(SMTP)连接时发生此错误。这个错误也表明,使用Windows的软件配置为传送到路由器,因为10065错误,如果Windows没有设置链接到一个路由器,而不是出现。

你应该确保网络路径正确,计算机没有运行两个软件防火墙和目标计算机未关闭。

+0

想让我知道在我有wriiten代码wats错误 – user2106954 2013-05-05 16:12:39

+0

什么是你正在越来越内在的例外..使用默认的身份验证为您的服务器我越来越关注exeption {“试图访问套接字的方式被其访问权限208.91.199.232:25“}禁止。检查你的内部异常,看看是什么 – 2013-05-05 16:21:59

+0

内部例外是{“一个套接字操作尝试到一个无法访问的网络208.91.199.230:25”} 和错误代码是10051 – user2106954 2013-05-05 16:36:39

0

发送邮件:

var smtp = new System.Net.Mail.SmtpClient(); 
{ 
    smtp.Host = "smtp.gmail.com"; 
    smtp.Port = 587; 
    smtp.EnableSsl = true; 
    smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; 
    smtp.Credentials = new NetworkCredential(fromAddress, fromPassword); 
    smtp.Timeout = 20000; 
} 
// Passing values to smtp object 
smtp.Send(fromAddress, toAddress, subject, body); 
+0

我想使用Windows Server而不是Gmail的SMTP发送邮件。 – user2106954 2013-05-05 16:18:07

0

使用此代码发送邮件,这是经过测试的代码。

public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body) 
    { 
     // Instantiate a new instance of MailMessage 
     MailMessage mMailMessage = new MailMessage(); 

     // Set the sender address of the mail message 
     mMailMessage.From = new MailAddress(from); 
     // Set the recepient address of the mail message 
     mMailMessage.To.Add(new MailAddress(to)); 

     // Check if the bcc value is null or an empty string 
     if ((bcc != null) && (bcc != string.Empty)) 
     { 
      // Set the Bcc address of the mail message 
      mMailMessage.Bcc.Add(new MailAddress(bcc)); 
     }  // Check if the cc value is null or an empty value 
     if ((cc != null) && (cc != string.Empty)) 
     { 
      // Set the CC address of the mail message 
      mMailMessage.CC.Add(new MailAddress(cc)); 
     }  // Set the subject of the mail message 
     mMailMessage.Subject = subject; 
     // Set the body of the mail message 
     mMailMessage.Body = body; 

     // Set the format of the mail message body as HTML 
     mMailMessage.IsBodyHtml = true; 
     // Set the priority of the mail message to normal 
     mMailMessage.Priority = MailPriority.Normal; 

     // Instantiate a new instance of SmtpClient 
     SmtpClient mSmtpClient = new SmtpClient(); 
     // Send the mail message 
     mSmtpClient.Host = "smtp.gmail.com"; //Or Your SMTP Server Address 
     mSmtpClient.Credentials = new System.Net.NetworkCredential 
      ("[email protected]", "Password"); 
     //Or your Smtp Email ID and Password 
     mSmtpClient.EnableSsl = true; 

     mSmtpClient.Send(mMailMessage); 
    } 
0

注:我一直在试图通过我的ISP的邮件服务器,但没有成功发送电子邮件 - 以下所有我能找到这些论坛上的建议。我刚刚发现,如果我不尝试更改默认凭据设置,它将正常工作。如果我包括:client.UseDefaultCredentials = true;或client.UseDefaultCredentials = false;在我的代码中,然后我会从服务器获取登录失败消息。通过不包括,它运作良好。

相关问题