C # Send email via common email

Source: Internet
Author: User
Tags mailmessage smtpclient
Using System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. Data;
Using System. drawing;
Using System. LINQ;
Using System. text;
Using System. Windows. forms;
Using System. net. mail;

NamespaceEmailtest
{
Public Partial ClassMsemail: Form
{
PrivateSmtpclient =NewSmtpclient ();

PublicMsemail ()
{
Initializecomponent ();

Combemailserver. selectedindex = 0 ;
}
///   <Summary>
/// Test email sending
///   </Summary>
///   <Param name = "sender"> </param>
///   <Param name = "E"> </param>
Private Void Btnsend_click ( Object Sender, eventargs E)
{
Mailmessage message = New Mailmessage ();

Try
{
Mailaddress fromaddress =NewMailaddress ("Sender's email address");

Message. From = fromaddress;
Message. Subject ="Alarm email";

Message. to. Add ("Target email address");

Message. Body ="Test email";

If (Combemailserver. Text = " Hotmail " )
{
Smtpclient. Host = " Smtp.live.com " ;
Smtpclient. enablessl = True ;
Smtpclient. usedefaultcredentials = True ;
}
Else If (Combemailserver. Text = " Gmail " )
{
Smtpclient. Host = " Smtp.gmail.com " ;
Smtpclient. Port = 587 ;
Smtpclient. enablessl = True ;
Smtpclient. usedefaultcredentials = True ;
}
Else If (Combemailserver. Text = " Qq mailbox " )
{
Smtpclient. Host = " Smtp.qq.com " ;
Smtpclient. Port = 25 ;
Smtpclient. enablessl = False ;
Smtpclient. usedefaultcredentials = True ;
}
Else If (Combemailserver. Text = " Email 163 " )
{
Smtpclient. Host = " Smtp.163.com " ;
Smtpclient. enablessl = False ;
Smtpclient. usedefaultcredentials = True ;
}
Else If (Combemailserver. Text = " Sina mail " )
{
Smtpclient. Host = " Smtp.sina.com " ;
Smtpclient. enablessl = False ;
Smtpclient. usedefaultcredentials = True ;
}

Smtpclient. Credentials = New System. net. networkcredential ( " Sender's email address " , " Sender's email password " );
Smtpclient. deliverymethod = smtpdeliverymethod. Network;
Smtpclient. sendcompleted + = New Sendcompletedeventhandler (smtpclient_sendcompleted );
Smtpclient. sendasync (message, combemailserver. Text );
}
Catch (Exception exm)
{
MessageBox. Show (exm. Message. tostring ());
}
}
///   <Summary>
/// Asynchronous sending completion event
///   </Summary>
///   <Param name = "sender"> </param>
///   <Param name = "E"> </param>
Void Smtpclient_sendcompleted ( Object Sender, asynccompletedeventargs E)
{
String stremailserver = ( String ) E. userstate;

If (E. Error! = Null )
{
MessageBox. Show (stremailserver + " Configuration failed " );
}
Else
{
MessageBox. Show ( " Sent successfully " );
}
}
}
}

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.