码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2019-05-08 09:28:44      阅读:334      评论:0      收藏:0      [点我收藏+]

标签:pass   text   com   asp   sage   发送   body   add   _id   

开发之余,把开发过程中比较常用的代码片段备份一次,下面的代码段是关于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",
"to_email@anydomain.com",
"This is email subject" ,
"This is email body",
Web.Mail.MailFormat.Text,
"Physical path to your Attachment")





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

标签:pass   text   com   asp   sage   发送   body   add   _id   

原文地址:https://www.cnblogs.com/Wladybird/p/10829375.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!