2013-10-24 109 views
1

我嘗試了兩個應用程序,一個在java中,一個在C#中。 Java應用程序可以成功發送電子郵件,但C#不能。下面是兩個應用: 1.JavaSmtpClient無法發送電子郵件

final String username = "[email protected]"; 
final String password = "mypassword"; 
String smtpHost = "smtp.mydomain"; 

Properties props = new Properties(); 
props.put("mail.smtp.auth", "true"); 
props.put("mail.smtp.host", smtpHost); 
props.put("mail.smtp.port", "465"); 
props.put("mail.smtp.socketFactory.port", "465"); 
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 

Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { 
    @Override 
    protected PasswordAuthentication getPasswordAuthentication() { 
     return new PasswordAuthentication(username, password); 
    } 
}); 

Message message = new MimeMessage(session); 
message.setFrom(new InternetAddress(username)); 
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(username)); 
message.setSubject("Test send email"); 
message.setText("Hi you!"); 
Transport.send(message); 

2.C#

string username = "[email protected]"; 
string password = "mypassword"; 
string smtpHost = "smtp.mydomain"; 

SmtpClient mailClient = new SmtpClient(smtpHost, 465); 
mailClient.Host = smtpHost; 
mailClient.Credentials = new NetworkCredential(username, password); 
mailClient.EnableSsl = true; 
MailMessage message = new MailMessage(username, username, "test send email", "hi u"); 

mailClient.Send(message); 

那麼,什麼是我在C#應用程序發出的錯誤呢?爲什麼它不能發送電子郵件?

編輯: 我已閱讀這個問題How can I send emails through SSL SMTP with the .NET Framework?它的工作原理。不推薦使用System.Web.Mail.SmtpMail,但System.Net.Mail.SmtpClient不適用。爲什麼?

3,本C#代碼做工精細:

System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage(); 
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver","smtp.mydomain"); 
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport","465"); 
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing","2"); 
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); 
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "[email protected]"); 
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "mypassword"); 
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true"); 
myMail.From = "[email protected]"; 
myMail.To = "[email protected]"; 
myMail.Subject = "new code"; 
myMail.BodyFormat = System.Web.Mail.MailFormat.Html; 
myMail.Body = "new body"; 
System.Web.Mail.SmtpMail.SmtpServer = "smtp.mydomain:465"; 
System.Web.Mail.SmtpMail.Send(myMail); 
+1

你會得到什麼樣的錯誤? – Mathew

+0

如果我使用代理,它會拋出「無法連接,因爲目標機器主動拒絕它[serverip]:465」。如果我不使用代理,它會拋出「操作超時。」。這兩個Java應用程序都可以工作。 – hieund

回答

0

你可以試試這個代碼。還在單獨的線程中發送電子郵件。

using System.Net.Mail; 

public static void Email(string Content, string To, string Subject, List<string> Attach = null, string User = "[email protected]", string Password = "MyPassword", string Host = "mail.sender.com", ushort Port = 587, bool SSL = false) 
{ 
    var Task = new System.Threading.Tasks.Task(() => 
    { 
     try 
     { 
      MailMessage Mail = new MailMessage(); 

      Mail.From = new MailAddress(User); 
      Mail.To.Add(To); 
      Mail.Subject = Subject; 
      Mail.Body = Content; 

      if (null != Attach) Attach.ForEach(x => Mail.Attachments.Add(new System.Net.Mail.Attachment(x))); 

      SmtpClient Server = new SmtpClient(Host); 

      Server.Port = Port; 
      Server.Credentials = new System.Net.NetworkCredential(User, Password); 
      Server.EnableSsl = SSL; 
      Server.Send(Mail); 
     } 
     catch (Exception e) 
     { 
      MessageBox.Show("Email send failed."); 
     } 
    }); 

    Task.Start(); 
} 
+0

它不起作用。 – hieund

+0

@Heiund請確保您的主機需要SSL(如Gmail)。除了端口應該是正確的。您的主機可能正在使用端口25.我一直在使用此代碼很長一段時間,沒有任何問題。 – Demir

1

.NET System.Net.Mail.SmtpClient類無法處理隱式SSL連接。隱式SSL連接通過端口465發送,因爲在您的示例中配置了客戶端。

您可以使用像AIM(Aegis Implicit Mail)這樣的第三方庫來發送隱式SSL消息。