Send Mail in ASP .NET (SMTP)

2020-07-20 03:15发布

问题:

I wrote the following code in my code file. But it does not work... plz help me! :)

protected void Button1_Click(object sender, EventArgs e)
{

    MailMessage msgeme = new MailMessage("someone@example.com", "someone@example.com", "subject", "body");
    SmtpClient smtpclient = new SmtpClient("smtp.gmail.com",587);
    smtpclient.EnableSsl = true;
    smtpclient.Send(msgeme);
    smtpclient.Credentials = new NetworkCredential("someone@example.com", "password");


}

I tried both 587 and 465. Bt it shows SMTP Exception handler error. Can anyone help???

回答1:

try to add DeliveryMethod with the different enumerations, and also credentials before send:

MailMessage msgeme = new MailMessage("someone@example.com", "someone@example.com", "subject", "body");
SmtpClient smtpclient = new SmtpClient("smtp.gmail.com",587);
smtpclient.EnableSsl = true;
smtpclient.Credentials = new NetworkCredential("someone@example.com", "password");
smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;

smtpclient.Send(msgeme);


回答2:

You need to set your credentials before you call the send method. More information about the error would be helpful though.



回答3:

 MailMessage msgeme = new MailMessage("someone@example.com", "someone@example.com", "subject", "body");
 SmtpClient smtpclient = new SmtpClient("smtp.gmail.com",587);
 smtpclient.EnableSsl = true;
 smtpclient.Credentials = new NetworkCredential("someone@example.com", "password");
 smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
 smtpclient.Send(msgeme);

Work !!! Thank you