C# SMTP email sending code fails for Yahoo Mail but works fine for other servers, can anyone help?

后端 未结 5 2033
孤城傲影
孤城傲影 2020-12-16 23:26

I am using this code to send an SMTP email via the yahoo SMTP server, it is for a personal project I am writing.

using System.Net.Mail;
using System.Net;

Sm         


        
相关标签:
5条回答
  • 2020-12-16 23:27

    I had the same problem until I set the port to 587 and disabled SSL.

    0 讨论(0)
  • 2020-12-16 23:32

    I think you should revert to using System.Web.Mail which lets you control fields that are not accessible through the newer System.Net. Try to play with those. For instance you could try this: (use is documented here, fields are documented here)

            MailMessage msg = new MailMessage();            
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.mail.yahoo.com");   
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
    
            // try "2", I have not tested for yahoo mail
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2");                                    
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "1");                        
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); // 0= anonymous - 1=basic - 2=NTLM
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "yahoousername");
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "yahoopwd");
    
    0 讨论(0)
  • 2020-12-16 23:35

    Port 465 isn't supported by System.Net.Mail.SmtpClient.

    http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl.aspx

    From the Remarks Section:

    This connection method is sometimes called SMTP/SSL, SMTP over SSL, or SMTPS and by default uses port 465. This alternate connection method using SSL is not currently supported.

    Edit: You could try using port 587 instead (if Yahoo supports it).

    0 讨论(0)
  • 2020-12-16 23:38

    It's not supported through 465, but the following post details a workaround

    How can I send emails through SSL SMTP with the .NET Framework?

    UPDATE: This link details why it might work through Outlook Express, but not through the System.Net.Mail

    http://blogs.msdn.com/b/webdav_101/archive/2008/06/02/system-net-mail-with-ssl-to-authenticate-against-port-465.aspx

    0 讨论(0)
  • 2020-12-16 23:43
    using System.Net.Mail;
    using System.Net;
    
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    
        private void btn_Send_Click(object sender, RoutedEventArgs e)
        {
            MailMessage oMail = new MailMessage(new MailAddress("username@yahoo.com"), new MailAddress("username@yahoo.com"));
            SmtpClient oSmtp = new SmtpClient();
            oSmtp.Host = "smtp.mail.yahoo.com";
            oSmtp.Credentials = new NetworkCredential("username", "password");
            oSmtp.EnableSsl = false;
            oSmtp.Port = 587;
            oSmtp.Send(oMail);
        }
    }
    
    0 讨论(0)
提交回复
热议问题