如何从Windows邮件服务器发送asp.net中的邮件

时间:2013-05-05 16:06:53

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

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

4 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

发送邮件:

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

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