zl程序教程

您现在的位置是:首页 >  后端

当前栏目

.netSMTP发送Email实例(可带附件)

实例 发送 email 附件
2023-06-13 09:15:02 时间
复制代码代码如下:

publicstaticvoidsendEmail(stringtoAddress,stringemailbody)
{
varfromAddress=ConfigurationManager.AppSettings["EmailAddress"];
stringfromPassword=ConfigurationManager.AppSettings["EmailPassword"].ToString();
conststringsubject="JobRecommendation";
varsmtp=newSmtpClient
{
Host=ConfigurationManager.AppSettings["SmtpServer"].ToString(),
Port=int.Parse(ConfigurationManager.AppSettings["SmtpPort"]),
EnableSsl=true,
DeliveryMethod=SmtpDeliveryMethod.Network,
UseDefaultCredentials=false,
Credentials=newNetworkCredential(fromAddress,fromPassword)
};
using(varmessage=newMailMessage(fromAddress,toAddress,subject,HttpUtility.HtmlEncode(emailbody)))
{
smtp.Send(message);
}
}
<addkey="EmailAddress"value="**********@gmail.com"/>//EmailAddress
<addkey="EmailPassword"value="*********"/>//EmialPWD
<addkey="SmtpServer"value="smtp.gmail.com"/>
<addkey="SmtpPort"value="587"/>
<--带附件版本->
varfromAddress="allenyinj@gmail.com";
stringfromPassword="yj1989120";
conststringsubject="CV";
varsmtp=newSmtpClient
{
Host="smtp.gmail.com",
Port=587,
EnableSsl=true,
DeliveryMethod=SmtpDeliveryMethod.Network,
UseDefaultCredentials=false,
Credentials=newNetworkCredential(fromAddress,fromPassword)
};
MailMessageemail=newMailMessage(fromAddress,"allen.yin.jun@gmail.com");
email.Subject="INLINEattachmentTEST";
email.IsBodyHtml=true;
stringattachmentPath="C:\\3.jpeg";
Attachmentinline=newAttachment(attachmentPath);
inline.ContentDisposition.Inline=true;
inline.ContentDisposition.DispositionType=DispositionTypeNames.Inline;
//inline.ContentId="1";
//inline.ContentType.MediaType="image/png";
inline.ContentType.Name=Path.GetFileName(attachmentPath);
email.Attachments.Add(inline);
email.Body="test";
smtp.Send(email);
email.Dispose();
//如果没有路径,用Stream
Attachmentletter=newAttachment(FileUploadLetter.FileContent,FileUploadLetter.PostedFile.ContentType);
letter.ContentDisposition.Inline=true;
letter.ContentDisposition.DispositionType=DispositionTypeNames.Inline;
//inline.ContentId="1";
letter.ContentType.MediaType=FileUploadLetter.PostedFile.ContentType;
letter.ContentType.Name=Path.GetFileName(FileUploadLetter.PostedFile.FileName);
letter.Name=Path.GetFileName(FileUploadLetter.PostedFile.FileName);