【问题标题】:How to send Mail in asp.net from windows mail server如何从 Windows 邮件服务器在 asp.net 中发送邮件
【发布时间】:2013-04-29 11:34:32
【问题描述】:

我已尝试从 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)

【问题讨论】:

    标签: c# asp.net c#-4.0 mail-server


    【解决方案1】:

    使用 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 错误。

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

    【讨论】:

    • 请你告诉我我写的代码有什么问题
    • 你得到的内部异常是什么......使用你的服务器的默认身份验证我得到以下异常{“尝试以访问权限 208.91 禁止的方式访问套接字。 199.232:25"}。检查你的内部异常,看看那是什么
    • 内部异常是 {"A socket operation was mapped to an unreachable network 208.91.199.230:25"} and Error Code is 10051
    【解决方案2】:

    发送邮件:

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

    【讨论】:

    • 我想使用 windows 服务器而不是 gmail smtp 发送邮件。
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      注意:我一直在尝试通过我的 ISP 邮件服务器发送电子邮件,但没有成功 - 遵循我在这些论坛上可以找到的所有建议。我刚刚发现,如果我不尝试更改默认凭据设置,它会正常工作。如果我包括任何一个:client.UseDefaultCredentials = true;或 client.UseDefaultCredentials = false;在我的代码中,然后我会从服务器收到登录失败消息。不包括任何一个,它都可以正常工作。

      【讨论】:

        猜你喜欢
        • 2013-06-12
        • 2015-11-27
        • 2012-11-19
        • 2019-06-20
        • 2011-01-26
        • 2015-05-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多