smtp发送带附件的邮件代码分享
网络编程 2021-07-04 22:41www.168986.cn编程入门
本文给大家分享一段smtp发送带附件邮件的代码,代码非常简洁易懂,有需要的小伙伴可以参考下。
该方式直接保存为HTML文件,也可以是文本文件,其它格式效果不是很好
代码如下:
MailMessage mmsg = new MailMessage();
mmsg.Subject = "邮件标题";
mmsg.Body = "邮件内容";
mmsg.To.Add("接收邮箱
byte[] bytes = System.Text.Encoding.Default.GetBytes
(@"<table><tr><td width=150>1234567891234567
</td><td width=80>12345678</td></tr></table>");
MemoryStream ms = new MemoryStream(bytes);
ContentType ct = new ContentType();
//附件文件类型
ct.MediaType = MediaTypeNames.Text.Html;
//附件名称,可以是其它后缀名
ct.Name = "附件名称" + DateTime.Now.ToString() + ".html";
mmsg.Attachments.Add(new Attachment(ms, ct));
//SMTP简单邮件协议
System.Net.Mail.SmtpClient sc
= new System.Net.Mail.SmtpClient();
sc.Host = "127.0.0.1";//主机地址
sc.Port = 25;//端口
//发送邮箱账号和密码
sc.Credentials =
new System.Net.NetworkCredential("aount", "password");
//发送邮箱
mmsg.From = new MailAddress("");
sc.Send(mmsg);
//释放流资源
ms.Close();
ms.Dispose();
另附上一个.使用smtp发送带附件邮件的实例
代码如下:
public static void sendEmail(string toAddress, string emailbody)
{
var fromAddress = ConfigurationManager.AppSettings["EmailAddress"];
string fromPassword = ConfigurationManager.AppSettings["EmailPassword"].ToString();
const string subject = "Job Remendation";
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);
}
}
<add key="EmailAddress" value=" Address
<add key="EmailPassword" value=""/> //Emial PWD
<add key="SmtpServer" value="smtp.gmail."/>
<add key="SmtpPort" value="587"/>
<--带附件版本->
var fromAddress = "";
string fromPassword = "yj1989120";
const string subject = "CV";
var smtp = new SmtpClient
{
Host = "smtp.gmail.",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress, fromPassword)
};
MailMessage email=new MailMessage(fromAddress, "");
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);
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程