0

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

4 回答 4

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);
    }
于 2013-05-06T10:12:20.213 回答
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);
于 2013-05-05T16:11:51.447 回答
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 错误。

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

于 2013-05-05T16:08:19.650 回答
0

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

于 2013-05-09T18:25:32.407 回答