Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the email code of smtp with attachments

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to write the email code of smtp sending belt attachment". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to write the email code of smtp sending belt attachment".

The copy code is as follows:

MailMessage mmsg = new MailMessage ()

Mmsg.Subject = "message title"

Mmsg.Body = "message content"

Mmsg.To.Add ("accept@qq.com"); / / receive mailbox

Byte [] bytes = System.Text.Encoding.Default.GetBytes

(@ "1234567891234567

12345678 ")

MemoryStream ms = new MemoryStream (bytes)

ContentType ct = new ContentType ()

/ / attachment file type

Ct.MediaType = MediaTypeNames.Text.Html

/ / attachment name, which can be another suffix

Ct.Name = "attachment name" + DateTime.Now.ToString () + ".html"

Mmsg.Attachments.Add (new Attachment (ms, ct))

/ / SMTP simple Mail Protocol

System.Net.Mail.SmtpClient sc

= new System.Net.Mail.SmtpClient ()

Sc.Host = "127.0.0.1"; / / Host address

Sc.Port = 2510 / Port

/ / send email account and password

Sc.Credentials =

New System.Net.NetworkCredential ("account", "password")

/ / send a mailbox

Mmsg.From = new MailAddress ("account@qq.com")

Sc.Send (mmsg)

/ / release stream resources

Ms.Close ()

Ms.Dispose ()

Attached is an example of sending mail with attachments using smtp. Net.

The copy code is as follows:

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)

}

}

/ / Email Address

/ / Emial PWD

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 ()

/ / if there is no path, use 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)

Thank you for your reading, the above is the content of "how to write the email code of smtp sending belt attachment". After the study of this article, I believe you have a deeper understanding of how to write the email code of smtp sending belt attachment, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report