System.Net.Mail和MailMessage不发送消息

时间:2017-11-09 18:15:39

标签: c# asp.net exchange-server

我已经从一个过时的名称空间更新到System.Net.Mail - 代码应该是直截了当的 - 我在发送电子邮件时遇到问题而且无法解决问题

public bool send()
{
    SmtpClient mailClient = new SmtpClient("my domain", 25);
    MailMessage mailMessage = new MailMessage();
    mailClient.EnableSsl = false;

    mailMessage.From = new MailAddress("my email");

    mailMessage.To.Add(sendTo.ToString());
    //mailMessage.Bcc.Add(bcc.ToString());
    //mailMessage.CC.Add(cc.ToString());

    mailMessage.Subject = subject;
    mailMessage.Body = body.ToString();
    mailMessage.IsBodyHtml = true;

    try
    {
        mailClient.Send(mailMessage);
    }
    catch (Exception exp)
    {
      exp.ToString();
      return false;
    }

    return true;
}

2 个答案:

答案 0 :(得分:0)

使用Gmail smtp服务器

var client = new SmtpClient();
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
if (!client.UseDefaultCredentials)
client.Credentials = new System.Net.NetworkCredential("your_email@mail.com", "your_email_pass");
client.EnableSsl = true;
client.Host = "smtp.gmail.com";
var mail = new MailMessage("from@mail.com", "to@mail.com");
mail.Subject = "test ";
mail.Body = "body message";
mail.IsBodyHtml = true;
client.Send(mail);

答案 1 :(得分:0)

smtp服务器没有凭据?或者你已经添加它。

尝试

mailClient.Credentials = new NetworkCredential("username", "password"),