本文为大家详细介绍下
net SMTP发送Email同时可带附件的具体实现思路及代码
想实现的朋友可以参考下哈
希望对大家有所帮助
复制代码 代码如下:
public static void sendEmail(string toAddress string emailbody)
{
var fromAddress = ConfigurationManagerAppSettings["EmailAddress"];
string fromPassword = ConfigurationManagerAppSettings["EmailPassword"]ToString();
const string subject = "Job Recommendation";
var smtp = new SmtpClient
{
Host = ConfigurationManagerAppSettings["SmtpServer"]ToString()
Port = intParse(ConfigurationManagerAppSettings["SmtpPort"])
EnableSsl = true
DeliveryMethod = SmtpDeliveryMethodNetwork
UseDefaultCredentials = false
Credentials = new NetworkCredential(fromAddress fromPassword)
};
using (var message = new MailMessage(fromAddress toAddress subject HttpUtilityHtmlEncode(emailbody)))
{
smtpSend(message);
}
}
<add key="EmailAddress" value="**********@gmailcom"/>//Email Address
<add key="EmailPassword" value="*********"/> //Emial PWD
<add key="SmtpServer" value="smtpgmailcom"/>
<add key="SmtpPort" value=""/>
<带附件版本>
var fromAddress = "allenyinj@gmailcom";
string fromPassword = "yj";
const string subject = "CV";
var smtp = new SmtpClient
{
Host = "smtpgmailcom"
Port =
EnableSsl = true
DeliveryMethod = SmtpDeliveryMethodNetwork
UseDefaultCredentials = false
Credentials = new NetworkCredential(fromAddress fromPassword)
};
MailMessage email=new MailMessage(fromAddress "allenyinjun@gmailcom");
emailSubject = "INLINE attachment TEST";
emailIsBodyHtml = true;
string attachmentPath = "C:jpeg";
Attachment inline = new Attachment(attachmentPath);
inlineContentDispositionInline = true;
inlineContentDispositionDispositionType = DispositionTypeNamesInline;
//inlineContentId = "";
//inlineContentTypeMediaType = "image/png";
inlineContentTypeName = PathGetFileName(attachmentPath);
emailAttachmentsAdd(inline);
emailBody = "test";
smtpSend(email);
emailDispose();
//如果没有路径用Stream
Attachment letter = new Attachment(FileUploadLetterFileContent FileUploadLetterPostedFileContentType);
letterContentDispositionInline = true;
letterContentDispositionDispositionType = DispositionTypeNamesInline;
//inlineContentId = "";
letterContentTypeMediaType = FileUploadLetterPostedFileContentType;
letterContentTypeName = PathGetFileName(FileUploadLetterPostedFileFileName);
letterName = PathGetFileName(FileUploadLetterPostedFileFileName);