如何使用gmail发送邮件

using System.Web.Mail;


   // Mail initialization
MailMessage mailMsg = new MailMessage();
mailMsg.From = "[email protected]";
mailMsg.To = "[email protected]";
//mailMsg.Cc = cc;
//mailMsg.Bcc = bcc;
mailMsg.Subject = "send mail use gmail";
mailMsg.BodyFormat = MailFormat.Text;
mailMsg.Body = "Hello here comes the sun";
mailMsg.Priority = MailPriority.High;
// Smtp configuration
SmtpMail.SmtpServer = "smtp.gmail.com";
// - smtp.gmail.com use smtp authentication
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "yourgmailaddress");
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "youpassword");
// - smtp.gmail.com use port 465 or 587
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
// - smtp.gmail.com use STARTTLS (some call this SSL)
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
// try to send Mail
try
{
    SmtpMail.Send(mailMsg);
    Response.Write("send success");
}
catch (Exception ex)
{
    Response.Write(ex.Message);
}

你可能感兴趣的:(Gmail)