SMTP Send message code sharing with attachments _ Practical Tips

Source: Internet
Author: User
Tags mailmessage smtpclient

This method is directly saved as an HTML file, or it can be a text file, other formats are not very good

Copy Code code as follows:

MailMessage mmsg = new MailMessage ();
Mmsg. Subject = "Mail title";
Mmsg. BODY = "Mail Content";
Mmsg. To.add ("accept@qq.com");/Receive Mailbox
byte[] bytes = System.Text.Encoding.Default.GetBytes
(@ "<table><tr><td width=150>1234567891234567
&LT;/TD&GT;&LT;TD width=80>12345678</td></tr></table> ");
MemoryStream ms = new MemoryStream (bytes);
ContentType ct = new ContentType ();
Attachment file type
Ct. mediatype = MediaTypeNames.Text.Html;
Attachment name, which can be another suffix name
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";//Hosts Address
Sc. Port = 25;//Ports
Send a mailbox account and password
Sc. Credentials =
New System.Net.NetworkCredential ("Account", "password");
Send a mailbox
Mmsg. from = new MailAddress ("account@qq.com");
Sc. Send (MMSG);
Releasing Flow resources
Ms. Close ();
Ms. Dispose ();

Attach a. NET to send an instance of a message with an attachment using SMTP

Copy Code code 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);
}
}
<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"/>
<--with Attachment version->
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 the 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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.