asp.net C # uses mailmessage to send mail code

Source: Internet
Author: User
Tags mail code mailmessage smtpclient

Example of MailMessage sending mail: C # helper class for sending mail The main thing is to use the Mail.mailmessage class under the Web namespace to send mail with this class is to set some properties on the instance of the MailMessage class, and then use SmtpMail to specify the SMTP server to send. The code is as follows:

The code is as follows Copy Code

<span id= "more-1794" ></span>

Using System;

Using System.Collections.Generic;

Using System.Linq;

Using System.Text;

Using System.Net.Mail;

Using System.Net.Mime;

Using System.IO;

Using System.Timers;

Using System.Xml;

Using System.Net;

Namespace TEST. Subunit

{

public class Emailhelper

{

Public MailMessage mailmessage (string fromaddress,string desemailuser, String toaddress, String emailSubject, String Emailtext)//Mail sender, support for mass, multiple addresses separated by a half-width comma

{

MailMessage mm = new MailMessage ();

Mm. To.add ("abc@qq.com");

Mm. To.add (toaddress)//temporary shielding

Cc

Mm. Cc. ADD (New MailAddress ("11@qq.com", "John", Encoding.GetEncoding (936));

Mm. Cc. ADD (New MailAddress ("22@qq.com", "Dick", Encoding.GetEncoding (936));

Mm. Cc. ADD (New MailAddress ("33@126.com", "Harry", Encoding.GetEncoding (936));

Mm. Bcc.add ("44@qq.com,44@qq.com");

Mm. from = new MailAddress (fromaddress, Desemailuser. Length&gt;0?desemailuser:fromaddress, Encoding.GetEncoding (936));

Mm. subjectencoding = encoding.getencoding (936);//Here is very important, if your mail title contains Chinese, here must be specified, otherwise the other side received very likely garbled.

Mm. Subject = EmailSubject; Message headers

Mm. Isbodyhtml = true; Whether the message body is HTML format

Mm. bodyencoding = encoding.getencoding (936); The message body code, set incorrectly, the receiver will receive garbled

//-------------------------------------------------------------------------

Emailtext + = "This is the mailbox to be sent correctly:" + toaddress;

Mm. BODY = emailtext;//Message text

Mm. Priority = Mailpriority.high; Message priority, divided into low, normal, high, usually with normal

Mm. Attachments.Add (New Attachment (@ "D:a.doc", SYSTEM.NET.MIME.MEDIATYPENAMES.APPLICATION.RTF));

The second parameter, which represents the file type of the attachment, can be used without specifying

return mm;

}

public bool SendEmail (string fromemailaddress, String toemailaddress,string emailtitle,string emailcontent,string Host , String port,string fromemailuser,string desemailuser,string fromemailpass)

{

BOOL B=false;

String message=string. Empty;

MailMessage mailessage =this.mailmessage (fromemailaddress,desemailuser,toemailaddress,emailtitle,emailcontent);

SmtpClient smtp = new SmtpClient (); Instantiate a SmtpClient

Smtp. Deliverymethod = Smtpdeliverymethod.network; Set SMTP Outbound to Network

Smtp. Enablessl = FALSE;//SMTP Whether SSL encryption is enabled on the server

Smtp. host = host; Specify SMTP server address

Smtp. Port = Int. Parse (port); Specifies the port of the SMTP server, which defaults to 25

Smtp. Credentials = new NetworkCredential (Fromemailuser, fromemailpass);//Certification

Try

{

Smtp. Send (Mailessage);

b = True;

Message= "Send success!"

}

catch (System.Net.Mail.SmtpException ex)

{

b = False;

Message= "Send failed!";

}

return b;

}

}

}

Example 2

The code is as follows Copy Code
Using systme.web.mail;//introduces namespaces
protected void Button1_Click (object sender, EventArgs e)
{
MailMessage mmsg = new MailMessage ();//Instance One MailMessage
Mmsg. Priority = mailpriority.low;//Set Priority level
Mmsg. from = "weicfprince@163.com";//Sender
Mmsg. to = "weicfprince@hotmail.com";//Recipient
Mmsg. BodyFormat = mailformat.text;//Body Format
Mmsg. Subject = "Mail Send Test";
Mmsg. BODY = "I sent a text message to myself in another mailbox";
Mmsg. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
Sender Mailbox Information
Mmsg. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/sendusername", "weicfprince");
Mmsg. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/sendpassword", "Hu Jintao");/password in this confidential haha
Smtpmail.smtpserver = "smtp.163.com";//Specify SMTP server
Try
{
Smtpmail.send (mmsg);//Send mail
Response.Write ("1");
}
catch (Exception ex)
{
Response.Write (ex. message);
}
}

Of course, you can also send attachments, using Mailattachments, which provides properties and methods to create a mail attachment object.
Mailattachments ma= New MailAttachment ("D://test.txt");/Send attachments to Mail
Mmsg. Attachments.Add (MA);//attach attachment to mail message object

The above is fully implemented to send mail function, but some mailboxes can not be sent, the reason is still not clear.

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.