C#和ASP.NET通过Gmail账户发送邮件的代码
2021-06-05 02:02
标签:pass text com asp sage 发送 body add _id 开发之余,把开发过程中比较常用的代码片段备份一次,下面的代码段是关于C#和ASP.NET通过Gmail账户发送邮件的代码,希望对大家有所用途。 using System.Web.Mail; myMail.Fields.Add System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465"; EmailLib.SendEmail.SendEmail("your_gmail_id", C#和ASP.NET通过Gmail账户发送邮件的代码 标签:pass text com asp sage 发送 body add _id 原文地址:https://www.cnblogs.com/Wladybird/p/10829375.html
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
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.Send(myMail);
return true;
}
catch (Exception ex)
{
throw;
}
}
}
"your_gmail_password",
"to_email@anydomain.com",
"This is email subject" ,
"This is email body",
Web.Mail.MailFormat.Text,
"Physical path to your Attachment")
下一篇:C#-NPOI操作EXCEL