SmtpClient无法发送电子邮件

时间:2013-10-24 08:09:31

标签: c# smtpclient

我尝试了两个应用程序,一个在java中,一个在C#中。 java应用程序可以成功发送电子邮件,但C#不能。这是两个应用程序: 1.Java

    final String username = "myaccount@mydomain";
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 = "myaccount@mydomain";
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", "myaccount@mydomain");
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "mypassword");
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
myMail.From = "myaccount@mydomain";
myMail.To = "myaccount@mydomain";
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);

2 个答案:

答案 0 :(得分:1)

.NET System.Net.Mail.SmtpClient类无法处理隐式SSL连接。通过端口465发送隐式SSL连接,因为在您的示例中配置了客户端。

您可以使用第三方库(如AIM(Aegis Implicit Mail))发送隐式SSL消息。

答案 1 :(得分:0)

您也可以尝试使用此代码。还可以在单​​独的帖子中发送电子邮件。

using System.Net.Mail;

public static void Email(string Content, string To, string Subject, List<string> Attach = null, string User = "sender@sender.com", 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();
}