I found a lot of email sending code on the Internet and found a connection problem. For example, TCP = new tcpclient (server, Port); server = smtp.126.com, Port = 25, cannot connect, in the following simple example, If you disable the firewall and McAfee antivirus software, you can send emails with attachments.
Using system;
Using system. Web. mail;
Namespace egxsun
{
///
/// Summary of systemmail.
///
Public class systemmail
{
Private string _ adminemail;
Private string _ smtpserver = "localhost ";
Private string _ password;
Private string _ username;
Public systemmail ()
{
}
Public String adminemail
{
Get {return _ adminemail ;}
Set {_ adminemail = value ;}
}
Public String smtpserver
{
Get {return _ smtpserver ;}
Set {_ smtpserver = value ;}
}
Public String Password
{
Get {return _ password ;}
Set {_ password = value ;}
}
Public String Username
{
Get {return _ username ;}
Set {_ username = value ;}
}
Public bool send (string to, string from, string subject, string message)
{
Try
{
Mailmessage em = new mailmessage ();
Em. To =;
Em. From = from;
Em. Subject = subject;
Em. Body = message;
Em. attachments. Add (New mailattachment ("FILENAME "));
// Found out how to send authenticated email via system. Web. Mail at http://SystemWebMail.com (fact 3.8)
If (this. Username! = NULL & this. Password! = NULL)
{
Em. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); // Basic Authentication
Em. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendusername", this. username); // set your username here
Em. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendpassword", this. Password); // set your password here
}
Smtpmail. smtpserver = This. smtpserver;
Smtpmail. Send (EM );
Return true;
}
Catch
{
Return false;
}
}
Public bool send (string to, string from, string subject, string body, string filename)
{
Try
{
Mailmessage em = new mailmessage ();
Em. To =;
Em. From = from;
Em. Subject = subject;
Em. Body = body;
Em. attachments. Add (New mailattachment (filename ));
// Found out how to send authenticated email via system. Web. Mail at http://SystemWebMail.com (fact 3.8)
If (this. Username! = NULL & this. Password! = NULL)
{
Em. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); // Basic Authentication
Em. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendusername", this. username); // set your username here
Em. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendpassword", this. Password); // set your password here
}
Smtpmail. smtpserver = This. smtpserver;
Smtpmail. Send (EM );
Return true;
}
Catch // (exception ex)
{
// String Ss = ex. tostring ();
Return false;
}
}
}
}
Usage:
Systemmail Sm = new systemmail ();
SM. smtpserver = "smtp.126.com ";
SM. Username = "******";
SM. Password = "******";
If (Sm. Send ("sunssss@126.com", "egxsun@126.com", "title", "body", @ "C:/deltable.rar "))
{
MessageBox. Show ("sent successfully! ");
}
Else
{
MessageBox. Show ("failed to send! ");
}