Use C # To send emails

Source: Internet
Author: User

Recently, some users raised a new demand, hoping that the company's ERP system will automatically send emails to the related personnel's mailbox after the transaction application is approved, so that they can know it immediately. Because they don't want to make one more call or drive ERP every day, but their outlook can automatically scan for a new email bounce reminder every five minutes. The user's requirements are not too high, so I will proceed.

There is a special system. Web. Mail class in vs2003 for processing. It is very simple to use and has been written before.

In vs2005, I upgraded to use a new system. net. Mail class. I recently learned about vs2005, So I abandoned the previous one.ProgramI want to show off the technology, but I don't have any technical skills.

Mailaddress from =   New Mailaddress ( " Gaosheng@hotmail.com " , " High " ); // Email sender

Mailmessage mail =   New Mailmessage ();

// Set the mail title
Mail. Subject = Txtsubject. text;

// Set the sender of the email
// Pass: if you do not want to display your email address, you can enter any name that matches the mail format. The real mail user is not set here. This is only used for display.
Mail. From = From;

// Set Email recipients
String Address =   "" ;
String Displayname =   "" ;
/*This is written here because it may be sent to multiple contacts, and each address is separated by a comma (;).
Generally, when you select a contact directly from the address book, The format is: username 1 <mail1>; username 2 <Mail 2>;
Therefore, the following logic is not good.Code
If you always need to send only one recipient, then simply mail. to. Add ("recipient mail ");
*/
String [] Mailnames = (Txtmailto. Text +   " ; " ). Split ( ' ; ' );
Foreach ( String Name In Mailnames)
{
If (Name ! =   String . Empty)
{
If (Name. indexof ( ' < ' ) >   0 )
{
Displayname=Name. substring (0, Name. indexof ('<'));
Address=Name. substring (name. indexof ('<')+ 1). Replace ('>',' ');
}
Else
{
Displayname= String. Empty;
Address=Name. substring (name. indexof ('<')+ 1). Replace ('>',' ');
}
Mail. to. Add ( New Mailaddress (address, displayname ));
}
}

//Set the CC recipient for the email
//This is much simpler. If you don't want to hurry up and lay off an important file, it would be better to send a CC copy to the leaders.
Mail. CC. Add (NewMailaddress ("Manage@hotmail.com","Dear leadership");

// set the email content
mail. body = txtbody. text;
// set the email format
mail. bodyencoding = system. text. encoding. utf8;
mail. isbodyhtml = true ;
// set the mail sending level
mail. priority = mailpriority. normal;

//Set the email attachment. The attachment selected on the client is uploaded to the server and saved to the mail.
StringFilename=Txtupfile. postedfile. filename. Trim ();
Filename= "D:/upfile/" +Filename. substring (filename. lastindexof ("/")+ 1);
Txtupfile. postedfile. saveas (filename );//Save the file to the server
Mail. attachments. Add (NewAttachment (filename ));

Mail. deliverynotificationoptions=Deliverynotificationoptions. onsuccess;

Smtpclient Client =   New Smtpclient ();
// Set the name of the host used for SMTP transaction, and enter the IP address.
Client. Host =   " Smtp.hotmail.com " ;
// Set the port used for SMTP transaction. The default value is 25.
// Client. Port = 25;
Client. usedefacrecredentials =   False ;
// Here is the real email login name and password. For example, my email address is hbgx @ hotmail, my username is hbgx, and my password is xgbh.
Client. Credentials =   New System. net. networkcredential ( " Hbgx " , " Xgbh " );
Client. deliverymethod = Smtpdeliverymethod. Network;
// All definitions are complete. The message is officially sent. It's very easy!
Client. Send (Mail );

In the past, I used the system. Web. Mail class to write a similar one. Now I have posted it. After all, I have spent a lot of effort studying it before.

// UseSystem. Web. Mail Statement Mailmessage mail =   New Mailmessage ();
Mail. = Txtmailto. text; // Recipients. Multiple recipients are separated by; numbers, which are flexible.
Mail. Subject = Txtsubject. text; // Topic
Mail. Body = Txtbody. text; // Content
Mail. bodyformat = Mailformat. html;
Mail. Priority = Mailpriority. normal;
// Displayed sender
Mail. Fields. Add ( " Http://schemas.microsoft.com/cdo/configuration/sendemailaddress " , " Gaosheng@hotmail.com " );
// Actual sender
Mail. Fields. Add ( " Http://schemas.microsoft.com/cdo/configuration/smtpaccountname " , " Hbgx@hotmail.com " );
// User Name
Mail. Fields. Add ( " Http://schemas.microsoft.com/cdo/configuration/sendusername " , " Hbgx " );
// Password
Mail. Fields. Add ( " Http://schemas.microsoft.com/cdo/configuration/sendpassword " , " Xgbh " );
Mail. Fields. Add ( " Http://schemas.microsoft.com/cdo/configuration/smtpauthenticate " , " 1 " ); // Verification level

Smtpmail. smtpserver =   " Smtp.hotmail.com " ;
Smtpmail. smtpserver. insert ( 0 , " Smtp.hotmail.com " );
Smtpmail. Send (Mail );

Is there a lot of flexibility in writing in the past? Of course it can be used now, because there is no conflict in use in different namespaces, but there is always a reason for upgrading, there are many differences between the two classes. Pay attention to them when using them.

I am a developer, not a dedicated lecturer. I am still criticizing and correcting the mistakes I have made. I am very grateful for sending messages, messages, or emails on csdn!

Well, there are still a lot of things to learn, so I will study here.

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.