smtp发送带附件的邮件代码分享_实用技巧
May 25, 2016 pm 05:06 PM
smtp
보내다
本文给大家分享一段smtp发送带附件邮件的代码,代码非常简洁易懂,有需要的小伙伴可以参考下。
该方式直接保存为HTML文件,也可以是文本文件,其它格式效果不是很好
代码如下:
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);
로그인 후 복사
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

인기 기사
스플릿 소설을이기는 데 얼마나 걸립니까?
3 몇 주 전
By DDD
Repo : 팀원을 부활시키는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
헬로 키티 아일랜드 어드벤처 : 거대한 씨앗을 얻는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
1 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌

인기 기사
스플릿 소설을이기는 데 얼마나 걸립니까?
3 몇 주 전
By DDD
Repo : 팀원을 부활시키는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
헬로 키티 아일랜드 어드벤처 : 거대한 씨앗을 얻는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
1 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌

뜨거운 기사 태그

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제
Gmail 이메일의 로그인 입구는 어디에 있나요?
7281
9


자바 튜토리얼
1622
14


Cakephp 튜토리얼
1341
46


라라벨 튜토리얼
1258
25


PHP 튜토리얼
1205
29



TikTok에서 다른 사람에게 파일을 보내는 방법은 무엇입니까? 다른 사람에게 보낸 파일을 삭제하는 방법은 무엇입니까?

HTML 양식 데이터를 텍스트로 가져와 html2pdf로 보내는 방법은 무엇입니까?

PHP 이메일 구문 분석 및 전송 기능에 대한 심층 소개: imap_open, imap_search, 메일 및 기타 기능에 대한 이메일 구문 분석 및 전송 기술

PHP 메일 사용 가이드: 간단하고 이해하기 쉬운 이메일 전송 튜토리얼

PHP를 사용하여 SMTP 프로토콜 기반 이메일 통신을 구현하는 방법
