您现在的位置是:网站首页> 编程资料编程资料
.net SMTP发送Email实例(可带附件)_实用技巧_
2023-05-24
308人已围观
简介 .net SMTP发送Email实例(可带附件)_实用技巧_
复制代码 代码如下:
public static void sendEmail(string toAddress, string emailbody)
{
var fromAddress = ConfigurationManager.AppSettings["EmailAddress"];
string fromPassword = ConfigurationManager.AppSettings["EmailPassword"].ToString();
const string subject = "Job Recommendation";
var smtp = new SmtpClient
{
Host = ConfigurationManager.AppSettings["SmtpServer"].ToString(),
Port = int.Parse(ConfigurationManager.AppSettings["SmtpPort"]),
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress, subject, HttpUtility.HtmlEncode(emailbody)))
{
smtp.Send(message);
}
}
<--带附件版本->
var fromAddress = "allenyinj@gmail.com";
string fromPassword = "yj1989120";
const string subject = "CV";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress, fromPassword)
};
MailMessage email=new MailMessage(fromAddress, "allen.yin.jun@gmail.com");
email.Subject = "INLINE attachment TEST";
email.IsBodyHtml = true;
string attachmentPath = "C:\\3.jpeg";
Attachment inline = new Attachment(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
Attachment letter = new Attachment(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);
相关内容
- asp.net Repeater 数据绑定的具体实现(图文详解)_实用技巧_
- VS2010 水晶报表的使用方法_实用技巧_
- Asp.net获取客户端IP常见代码存在的伪造IP问题探讨_实用技巧_
- Asp.Net 无刷新文件上传并显示进度条的实现方法及思路_实用技巧_
- HTTP错误500.19解决方法(定义了重复的节点)_实用技巧_
- Gridview用法大总结(全程图解珍藏版)_实用技巧_
- asp.net中javascript的引用(直接引入和间接引入)_实用技巧_
- DataGridView - DataGridViewCheckBoxCell的使用介绍_实用技巧_
- 三层+存储过程实现分页示例代码_实用技巧_
- ASP.NET中操作SQL数据库(连接字符串的配置及获取)_实用技巧_
