【问题标题】:SmtpClient cannot send emailSmtpClient 无法发送电子邮件
【发布时间】:2015-04-07 17:25:49
【问题描述】:

我尝试了两个应用程序,一个在 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);

【问题讨论】:

  • 你得到什么样的错误?
  • 如果我使用代理,它会抛出“无法建立连接,因为目标机器主动拒绝它 [serverip]:465”。如果我不使用代理,它会抛出“操作已超时。”。 Java 应用程序在两者上都可以工作。

标签: c# smtpclient


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    你也可以试试这个代码。还会在单独的线程中发送电子邮件。

    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();
    }
    

    【讨论】:

    • @Heiund 请确保您的主机是否需要 SSL(如 Gmail)。除了端口应该是正确的。您的主机可能正在使用端口 25。我已经使用此代码很长时间了,没有任何问题。
    猜你喜欢
    • 2015-06-11
    • 2015-02-26
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 2020-03-20
    相关资源
    最近更新 更多