通过SSL设置发送电子邮件

时间:2015-03-03 11:07:08

标签: c# .net ssl

如果我在不启用SSL的情况下发送,我可以使用以下代码发送电子邮件。当我启用SSL时,我必须更改端口和传出服务器等凭据。已多次检查凭据并且它们是正确的,并且还确认了我的托管公司的凭据。我错过了阻止我通过SSL发送邮件的任何步骤。请指教谢谢。

//Sending without SSL which works
public static string error;
public static void Send()
{
    try
    {
        MailMessage mail = new MailMessage(); 
        SmtpClient SmtpServer = new SmtpClient("mail.mywebsite.com"); 

        mail.From = new MailAddress("management@mywebsite.com");
        mail.To.Add("report@mywebsite.com");

        mail.Subject = "Inventory Status/ Update";
        mail.Body = Connection.emailUpdate;

        SmtpServer.Port = 26;
        SmtpServer.Credentials = new System.Net.NetworkCredential("management@mywebsite.com", "password");
                SmtpServer.EnableSsl = false;

        SmtpServer.Send(mail);
        SmtpServer.Timeout = 10000;
        MessageBox.Show("mail Send");
    }
    catch (Exception ex)
    {
        error = ex.ToString();
    }
}

//Sending with SSL which fails
public static string error;
public static void Send()
{
    try
    {
        MailMessage mail = new MailMessage(); 
        SmtpClient SmtpServer = new SmtpClient("eu2.01domain.net");//Change in outgoing server 

        mail.From = new MailAddress("management@mywebsite.com");
        mail.To.Add("report@mywebsite.com");

        mail.Subject = "Inventory Status/ Update";
        mail.Body = Connection.emailUpdate;

        SmtpServer.Port = 465; //Change in port
        SmtpServer.Credentials = new System.Net.NetworkCredential("management@mywebsite.com", "password");
        SmtpServer.EnableSsl = true; //SSL enabled

        SmtpServer.Send(mail);
        SmtpServer.Timeout = 10000;
        MessageBox.Show("mail Send");
    }
    catch (Exception ex)
    {
        error = ex.ToString();
    }
}

0 个答案:

没有答案