C # send an email with an attachment

Source: Internet
Author: User
Tags mailmessage

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

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.