如何编写smtp发送带附件的邮件代码

发布时间:2021-09-29 10:20:19 作者:iii
来源:亿速云 阅读:111

这篇文章主要讲解了“如何编写smtp发送带附件的邮件代码”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何编写smtp发送带附件的邮件代码”吧!

复制代码 代码如下:   

    MailMessage mmsg = new MailMessage();
    mmsg.Subject = "邮件标题";
    mmsg.Body = "邮件内容";
    mmsg.To.Add("accept@qq.com");//接收邮箱
    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("account", "password");
    //发送邮箱
    mmsg.From = new MailAddress("account@qq.com");
    sc.Send(mmsg);
    //释放流资源
    ms.Close();
    ms.Dispose();

另附上一个.net使用smtp发送带附件邮件的实例

复制代码 代码如下:

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);
}
}
<add key="EmailAddress" value="**********@gmail.com"/>//Email Address
<add key="EmailPassword" value="*********"/> //Emial PWD
<add key="SmtpServer" value="smtp.gmail.com"/>
<add key="SmtpPort" value="587"/>
<--带附件版本->
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);

感谢各位的阅读,以上就是“如何编写smtp发送带附件的邮件代码”的内容了,经过本文的学习后,相信大家对如何编写smtp发送带附件的邮件代码这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. python使用smtp的587端口来自动发送带附件的邮件
  2. Python3如何使用SMTP发送带附件邮件?

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

smtp

上一篇:MongoDB数据量大报表慢该怎么办以及怎么做分页?

下一篇:如何理解Laravle eloquent多对多模型关联

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》