Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write the email code of smtp with attachments

Shulou Source: shulou.com Published: 2022-06-03 09:08:51 09月20日 Update

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!

Tags: Code mail attachment content mailbox learning name host suffix address instance password that is ideas situations files articles more title knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Redmi OPPO Reno Linux Shulou Tech Info