2016-08-08 65 views
-4
using System.Net.Mail; 

protected void SendMail() 
{ 
    try 
    { 
     MailMessage mail = new MailMessage(); 
     SmtpClient SmtpServer = new SmtpClient("smtp.google.com"); 
     SmtpServer.Timeout = 30000; 
     SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network; 
     mail.From = new MailAddress("[email protected]"); 
     mail.To.Add("[email protected]"); 
     mail.Subject = "test"; 
     mail.Body = "test"; 
     mail.Priority = MailPriority.High; 

     SmtpServer.Port = 587;//25 
     SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "pwd"); 
     SmtpServer.EnableSsl = true; 
     SmtpServer.UseDefaultCredentials = false; 

     SmtpServer.Send(mail); 
     //MessageBox.Show("mail Send"); 
    } 
    catch (Exception ex) 
    { 
     //MessageBox.Show(ex.Message.ToString()); 
    } 
} 

我还没有在我的代码中发现任何错误,因为在互联网上的几个来源。这仍然没有解决。发送邮件在asp .net

+3

最没用的东西说是_it不工作_你能详细说明什么是不工作?任何错误消息? – Steve

+2

@Steve对我来说,更无用的事情是捕捉并吞下异常。 –

+0

如果有任何例外,请告诉我们吗? –

回答

0

尝试此方法

在Gmail中使用端口25和SMTP-smtp.gmail.com

public void mail(string FromEmail, string FromPass, string To, string Tocc, string Tobcc, string subject, string message, string smtpadd, int portnum) 
     { 
      try 
      { 
       System.Net.Mail.SmtpClient st = new System.Net.Mail.SmtpClient(smtpadd); 
       System.Net.Mail.MailMessage mst = new System.Net.Mail.MailMessage(); 
       mst.To.Add(To); 
       if (Tocc != "") 
       { 
        mst.CC.Add(Tocc); 
       } 
       if (Tobcc != "") 
       { 
        mst.Bcc.Add(Tobcc); 
       } 
       mst.IsBodyHtml = true; 
       mst.From = new System.Net.Mail.MailAddress(FromEmail); 
       mst.Subject = subject; 
       mst.Body = message; 
       System.Net.NetworkCredential nc = new System.Net.NetworkCredential(FromEmail, FromPass); 
       st.UseDefaultCredentials = true; 
       st.EnableSsl = true; 
       st.Port = portnum; 
       st.Credentials = nc; 

       st.Send(mst); 
      } 
      catch (Exception e) 
      { 
      } 
     } 
+0

再次,一个空的catch块代码片段!为什么人们不断地写和发布这样的代码? –