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