简体   繁体   中英

System.Net.Mail and MailMessage not Sending Messages

I have update from an obsolete name space to System.Net.Mail - the code was supposed to be straight forward - I am having problems sending email and can't get hold of the issue

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

using Gmail smtp server

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

No Credential for the smtp server? Or you add it already.

Try

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM