如何在asp.net中从windows邮件服务器发送邮件

本文关键字:邮件服务器 windows 中从 asp net | 更新日期: 2023-09-27 18:12:07

我试过从gmail和其他共享邮件服务器发送邮件,其工作正常但是现在我的要求是从windows邮件服务器发送邮件。每当我尝试发送邮件时,它显示给我发送邮件失败错误。

SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Host = "us2.webmail.mailhostbox.com";
string fromaddress = "info@csi.com";
client.Port = 25;
//client.Host = "mail.csisite.com";
// setup Smtp authentication
System.Net.NetworkCredential credentials =
    new System.Net.NetworkCredential("info@csisite.com", "password");
//client.UseDefaultCredentials = true;
client.Credentials = credentials;
client.EnableSsl = false;
try
{
//mail For Customer
MailMessage msgcustomer = new MailMessage();
msgcustomer.From = new MailAddress(fromaddress);
msgcustomer.To.Add(new MailAddress(EmailID));
msgcustomer.Subject = "Thank you for writing to us" ;
msgcustomer.IsBodyHtml = true;
msgcustomer.Body = "Test Mail";
client.Send(msgcustomer)

如何在asp.net中从windows邮件服务器发送邮件

Send Mail:

var smtp = new System.Net.Mail.SmtpClient();
{
    smtp.Host = "smtp.gmail.com";
    smtp.Port = 587;
    smtp.EnableSsl = true;
    smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
    smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
    smtp.Timeout = 20000;
}
// Passing values to smtp object
smtp.Send(fromAddress, toAddress, subject, body);

using System.Net.Mail

    MailMessage msgMail = new MailMessage();
    MailMessage myMessage = new MailMessage();
    myMessage.From = new MailAddress("sender's email","sender`s name and surname");
    myMessage.To.Add("recipient's email");
    myMessage.Subject = "Subject";
    myMessage.IsBodyHtml = true;
    myMessage.Body = "Message Body";

    SmtpClient mySmtpClient = new SmtpClient();
    System.Net.NetworkCredential myCredential = new System.Net.NetworkCredential("email", "password");
    mySmtpClient.Host = "your smtp host address";
    mySmtpClient.UseDefaultCredentials = false;
    mySmtpClient.Credentials = myCredential;
    mySmtpClient.ServicePoint.MaxIdleTime = 1;
    mySmtpClient.Send(myMessage);
    myMessage.Dispose();

更新后的用户得到以下错误

{"尝试对不可达网络进行套接字操作208.91.199.230:25"},错误码为10051

错误10051是当无法到达目标网络或主机服务器时出现的套接字错误。此错误可能是由于路由器配置错误、Internet断开连接或防火墙的阻止操作而发生的。此错误主要发生在尝试使用错误的配置生成Internet控制消息协议(ICMP)和简单邮件传输协议(SMTP)连接时。此错误还表明所使用的Windows软件被配置为与路由器通信,因为如果Windows未设置为链接到路由器,则出现10065错误。

您应该确保网络路径是正确的,计算机没有运行两个软件防火墙,并且目标计算机没有关闭。

使用此代码发送邮件这是经过测试的代码。

 public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
    {
        // Instantiate a new instance of MailMessage
        MailMessage mMailMessage = new MailMessage();
        // Set the sender address of the mail message
        mMailMessage.From = new MailAddress(from);
        // Set the recepient address of the mail message
        mMailMessage.To.Add(new MailAddress(to));
        // Check if the bcc value is null or an empty string
        if ((bcc != null) && (bcc != string.Empty))
        {
            // Set the Bcc address of the mail message
            mMailMessage.Bcc.Add(new MailAddress(bcc));
        }      // Check if the cc value is null or an empty value
        if ((cc != null) && (cc != string.Empty))
        {
            // Set the CC address of the mail message
            mMailMessage.CC.Add(new MailAddress(cc));
        }       // Set the subject of the mail message
        mMailMessage.Subject = subject;
        // Set the body of the mail message
        mMailMessage.Body = body;
        // Set the format of the mail message body as HTML
        mMailMessage.IsBodyHtml = true;
        // Set the priority of the mail message to normal
        mMailMessage.Priority = MailPriority.Normal;
        // Instantiate a new instance of SmtpClient
        SmtpClient mSmtpClient = new SmtpClient();
        // Send the mail message
        mSmtpClient.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
        mSmtpClient.Credentials = new System.Net.NetworkCredential
             ("YourEmail@gmail.com", "Password");
        //Or your Smtp Email ID and Password
        mSmtpClient.EnableSsl = true;
        mSmtpClient.Send(mMailMessage);
    }

注意:我一直试图通过我的ISP邮件服务器发送电子邮件没有成功-以下所有的建议,我可以在这些论坛上找到。我刚刚发现,如果我不尝试更改默认凭据设置,它可以正常工作。如果我包括:客户。UseDefaultCredentials = true;或客户端。UseDefaultCredentials = false;在我的代码中,那么我将从服务器获得登录失败消息。不包括这两个,它工作得很好。