Can someone help me with the following code? I am trying to send out gmail from my asp.net webform. here is what I have so far. Thanks so much....
Code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.Net.Mail;
public partial class home : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public bool SendMail(string gMailAccount, string password, string to, string subject, string message)
{
try
{
NetworkCredential loginInfo = new NetworkCredential(gMailAccount, password);
MailMessage msg = new MailMessage();
msg.From = new MailAddress(gMailAccount);
msg.To.Add(new MailAddress(to));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
client.Send(msg);
return true;
}
catch (Exception)
{
return false;
}
}
protected void btnSend_Click(object sender, EventArgs e)
{
if (SendMail("my@gmail.com", "mypassword", "to@gmail.com", "This is a my subject", "This is my message"))
{
lblStatus.Text = "Sent";
}
else
{
lblStatus.Text = "Not Sent";
}
}
}
Comment