C#和ASP.NET通过Gmail账户发送邮件的代码

开发之余,把开发过程中比较常用的代码片段备份一次,下面的代码段是关于C#和ASP.NET通过Gmail账户发送邮件的代码,希望对大家有所用途。

using System.Web.Mail;
using System;
public class MailSender
{
public static bool SendEmail(
string pGmailEmail,
string pGmailPassword,
string pTo,
string pSubject,
string pBody,
System.Web.Mail.MailFormat pFormat,
string pAttachmentPath)
{
try
{
System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
myMail.Fields.Add
"smtp.gmail.com");
myMail.Fields.Add
"465");
myMail.Fields.Add
"2");

myMail.Fields.Add
myMail.Fields.Add
pGmailEmail);
myMail.Fields.Add
pGmailPassword);
myMail.Fields.Add
"true");
myMail.From = pGmailEmail;
myMail.To = pTo;
myMail.Subject = pSubject;
myMail.BodyFormat = pFormat;
myMail.Body = pBody;
if (pAttachmentPath.Trim() != "")
{
MailAttachment MyAttachment =
new MailAttachment(pAttachmentPath);
myMail.Attachments.Add(MyAttachment);
myMail.Priority = System.Web.Mail.MailPriority.High;
}

System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465";
System.Web.Mail.SmtpMail.Send(myMail);
return true;
}
catch (Exception ex)
{
throw;
}
}
}

EmailLib.SendEmail.SendEmail("your_gmail_id",
"your_gmail_password",
"[email protected]",
"This is email subject" ,
"This is email body",
Web.Mail.MailFormat.Text,
"Physical path to your Attachment")





猜你喜欢

转载自www.cnblogs.com/Wladybird/p/10829375.html