. Net SMTP sends an Email with an attachment example

Source: Internet
Author: User
Tags mailmessage smtpclient

Copy codeThe Code is as follows:
Public static void sendEmail (string toAddress, string emailbody)
{
Var fromAddress = ConfigurationManager. receivettings ["EmailAddress"];
String fromPassword = ConfigurationManager. receivetask[ "EmailPassword"]. ToString ();
Const string subject = "Job Recommendation ";


Var smtp = new SmtpClient
{
Host = ConfigurationManager. receivettings ["SmtpServer"]. ToString (),
Port = int. Parse (ConfigurationManager. receivettings ["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"/>

<-- Version with attachment->

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

// If you want to send an anonymous email

Var smtp = new SmtpClient
{
Host = "serverName ",
Port = 25, // anonymous sending Port
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod. Network,
UseDefaultCredentials = false,
// Credentials = new NetworkCredential (fromAddress, fromPassword)
};

Related Article

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.