Giri
Giri

Reputation: 65

Send Mail in ASP .NET (SMTP)

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("[email protected]", "[email protected]", "subject", "body");
    SmtpClient smtpclient = new SmtpClient("smtp.gmail.com",587);
    smtpclient.EnableSsl = true;
    smtpclient.Send(msgeme);
    smtpclient.Credentials = new NetworkCredential("[email protected]", "password");


}

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

Upvotes: 4

Views: 4452

Answers (3)

Roengsak Kaewpengkro
Roengsak Kaewpengkro

Reputation: 11

 MailMessage msgeme = new MailMessage("[email protected]", "[email protected]", "subject", "body");
 SmtpClient smtpclient = new SmtpClient("smtp.gmail.com",587);
 smtpclient.EnableSsl = true;
 smtpclient.Credentials = new NetworkCredential("[email protected]", "password");
 smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
 smtpclient.Send(msgeme);

Work !!! Thank you

Upvotes: 1

Shlomi Komemi
Shlomi Komemi

Reputation: 5545

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

MailMessage msgeme = new MailMessage("[email protected]", "[email protected]", "subject", "body");
SmtpClient smtpclient = new SmtpClient("smtp.gmail.com",587);
smtpclient.EnableSsl = true;
smtpclient.Credentials = new NetworkCredential("[email protected]", "password");
smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;

smtpclient.Send(msgeme);

Upvotes: 2

BentOnCoding
BentOnCoding

Reputation: 28148

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

Upvotes: 3

Related Questions