but the only problem that I face is authentication, so please provide an explicit example.
Let us say for instance that my e-mail (from field) is: "from@yahoo.com" with user name: "from" and password: "password", and I am sending to: "to@hotmail.com".
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); message.To.Add("luckyperson@online.microsoft.com"); message.Subject = "This is the Subject line"; message.From = new System.Net.Mail.MailAddress("From@online.microsoft.com"); message.Body = "This is the message body"; System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost"); smtp.Send(message);
I was very interested to try the code snippet using authentificated SMTP you gave us, but I cannot find and member Fields in the System.Web.Mail.MailMessage object.
How come I am the only one Did I do something wrong
// Put the message in the Drafts folder of the sender's mailbox. WebResponse putResponse = (HttpWebResponse)putRequest.GetResponse(); putResponse.Close();
// Request to move the email from the drafts to the mail submission Uri. HttpWebRequest moveRequest = (HttpWebRequest)HttpWebRequest.Create(draftsUri); moveRequest.Credentials = credentials; moveRequest.Method = "MOVE"; moveRequest.Headers.Add("Destination", submissionUri);
// Put the message in the mail submission folder. WebResponse moveResponse = (HttpWebResponse)moveRequest.GetResponse(); moveResponse.Close(); }
Dear Josh, Thank you very much for the great post. It really demonstrates everything easily. Hope that you wouldn't mind some minor notes about it anyway: 1-The namespace System.Net.Mail is a part of the class library of .NET framwork 2.0 (currently in BETA version), to send mail using .NET framework 1.1, you'd use System.Web.Mail instead.. 2-There's no class called SmtpClient in the System.Web.Mail namspace, but it's a member of the System.Net.Mail namspace in .NET Framework 2.0.
To send mail via Microsoft .NET Framework 1.1 (the current version) you can use:
System.Web.Mail.MailMessage message=
new System.Web.Mail.MailMessage(); message.From="from e-mail"; message.To="to e-mail"; message.Subject="Message Subject"; message.Body="Message Body"; System.Web.Mail.SmtpMail.SmtpServer="SMTP Server Address"; System.Web.Mail.SmtpMail.Send(message);
Now, in fact, both segments of code will not work for most SMTP hosts. This is because most SMTP hosts today require authentication (user/pass) to allow you to use the server. To send mail using Authenticated SMTP, many people use third party tools. There are many out there and some of them are good and free, but, System.Web.Mail doesn't need that really. You can send mail via Authenticated SMTP mail servers using the 1st code I wrote, just add the bold lines to it to be:
Is this for sending mail from a web page or from a Windows Forms application Thanks!
Josh Ledgard wrote:
Here is the code snippet I found...
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); message.To.Add("luckyperson@online.microsoft.com"); message.Subject = "This is the Subject line"; message.From = new System.Net.Mail.MailAddress("From@online.microsoft.com"); message.Body = "This is the message body"; System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost"); smtp.Send(message);
How do I send mail using C#?
Giacomo Citi
I have McAffee Scan On-Access disabled and problem is gone
greetz
Kevin2
This was helful,
...
but the only problem that I face is authentication, so please provide an explicit example.
Let us say for instance that my e-mail (from field) is: "from@yahoo.com" with user name: "from" and password: "password", and I am sending to: "to@hotmail.com".
That is what I am trying to do for months
Thanks a lot
Tiffany1489
Here is the code snippet I found...
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("luckyperson@online.microsoft.com");
message.Subject = "This is the Subject line";
message.From = new System.Net.Mail.MailAddress("From@online.microsoft.com");
message.Body = "This is the message body";
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");
smtp.Send(message);
Alexandre Siniouguine
private
void SendToAnalyst(){
string emailSubject = string.Empty; string emailAddress = string.Empty;StringBuilder sb =
new StringBuilder(); //To Do: Instantiate Monitoring Component if(mSourceSystem.ToUpper().Equals("FLEXCAB")){
emailSubject = "Sample RequestId (FLEXCAB To RADTI RESPONSE): "+mSampleRequestID;
sb.Append("**************************************************************************************\n");
sb.Append("\n");
sb.Append("Please do not reply to this email. This is a System generated email.\n");
sb.Append("\n");
sb.Append("**************************************************************************************\n");
sb.Append("\n");
sb.Append("Sample Request ID: "+mSampleRequestID+"\n");
sb.Append("Request Status: "+ mReqStatus+"\n");
sb.Append("\n");
sb.Append("Please Correct the error and re-submit.\n");
sb.Append("Or Call the EDS DBA on 03-8866-7424\n");
//Console.WriteLine(sb.ToString()); // Send the email body to Monitoring component}
}
The send mail class I am trying to create should catch the parameters from this SendToAnalyst class.
kaarde
Dear Mohamed,
I was very interested to try the code snippet using authentificated SMTP you gave us, but I cannot find and member Fields in the System.Web.Mail.MailMessage object.
How come I am the only one Did I do something wrong
Thanks,
RW098
Not that everyone cares, just that maybe one or two do. Here's how to post an email to an Exchange Server mailbox over http using WebDAV.
public static void SendEmail(string server, string mailbox, string password, string to, string subject, string body, bool secureHttp)
{
string httpProtocol = secureHttp "https://" : "http://";
string mailboxUri = httpProtocol + server + "/" + mailbox;
string submissionUri = httpProtocol + server + "/" + mailbox + "/##DavMailSubmissionURI##/";
string draftsUri = "http://" + server + "/" + mailbox + "/drafts/" + subject + ".eml";
string message = "To: " + to + "\n" +
"Subject: " + subject + "\n" +
"Date: " + System.DateTime.Now +
"X-Mailer: mailer" + "\n" +
"MIME-Version: 1.0" + "\n" +
"Content-Type: text/plain;" + "\n" +
"Charset = \"iso-8859-1\"" + "\n" +
"Content-Transfer-Encoding: 7bit" + "\n" +
"\n" + body;
// Credentials for exchange requests.
CredentialCache credentials = new CredentialCache();
credentials.Add(new Uri(mailboxUri), "NTLM", new NetworkCredential(mailbox, password));
// Request to put an email the drafts folder.
HttpWebRequest putRequest = (HttpWebRequest)HttpWebRequest.Create(draftsUri);
putRequest.Credentials = credentials;
putRequest.Method = "PUT";
putRequest.ContentType = "message/rfc822";
byte[] bytes = Encoding.UTF8.GetBytes((string)message);
putRequest.ContentLength = bytes.Length;
Stream putRequestStream = putRequest.GetRequestStream();
putRequestStream.Write(bytes, 0, bytes.Length);
putRequestStream.Close();
// Put the message in the Drafts folder of the sender's mailbox.
WebResponse putResponse = (HttpWebResponse)putRequest.GetResponse();
putResponse.Close();
// Request to move the email from the drafts to the mail submission Uri.
HttpWebRequest moveRequest = (HttpWebRequest)HttpWebRequest.Create(draftsUri);
moveRequest.Credentials = credentials;
moveRequest.Method = "MOVE";
moveRequest.Headers.Add("Destination", submissionUri);
// Put the message in the mail submission folder.
WebResponse moveResponse = (HttpWebResponse)moveRequest.GetResponse();
moveResponse.Close();
}
satheeshkumarana
I use your code at the office it is working fine, but at home i get with te same code an error:
SmtpException was unhandled Failure sending mail.
I use a winXP Pro SP2 only the smtp server is different
can you help me
thanks
redspider
francoisL
Dear Josh,
Thank you very much for the great post. It really demonstrates everything easily. Hope that you wouldn't mind some minor notes about it anyway:
1-The namespace System.Net.Mail is a part of the class library of .NET framwork 2.0 (currently in BETA version), to send mail using .NET framework 1.1, you'd use System.Web.Mail instead..
2-There's no class called SmtpClient in the System.Web.Mail namspace, but it's a member of the System.Net.Mail namspace in .NET Framework 2.0.
To send mail via Microsoft .NET Framework 1.1 (the current version) you can use:
System.Web.Mail.MailMessage message=
new System.Web.Mail.MailMessage();message.From="from e-mail";
message.To="to e-mail";
message.Subject="Message Subject";
message.Body="Message Body";
System.Web.Mail.SmtpMail.SmtpServer="SMTP Server Address";
System.Web.Mail.SmtpMail.Send(message);
System.Web.Mail.SmtpMail.SmtpServer="SMTP Host Address";
System.Web.Mail.SmtpMail.Send("from","To","Subject","MessageText");
Now, in fact, both segments of code will not work for most SMTP hosts. This is because most SMTP hosts today require authentication (user/pass) to allow you to use the server. To send mail using Authenticated SMTP, many people use third party tools. There are many out there and some of them are good and free, but, System.Web.Mail doesn't need that really. You can send mail via Authenticated SMTP mail servers using the 1st code I wrote, just add the bold lines to it to be:
System.Web.Mail.MailMessage message=
new System.Web.Mail.MailMessage();message.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1 );
message.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendusername","SmtpHostUserName" );
message.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendpassword","SmtpHostPassword" );
message.From="from e-mail";
message.To="to e-mail";
message.Subject="Message Subject";
message.Body="Message Body";
System.Web.Mail.SmtpMail.SmtpServer="SMTP Server Address";
System.Web.Mail.SmtpMail.Send(message);
For more detailed information on sending mail using .NET framework 1.1, check this detailed FAQ (take care, it's often detailed more than you may need!).
For information on the System.Net.Mail namespace in Microsoft.NET Framework 2.0, you may check Visual Studio 2005 Library > .NET Framework Reference > Class Library > System.Net.Mail.
Wish to hear if this could help
Regards,
Kojacked
Geoffers
I forget to ask how to get the SMTP name of my mail server
Is it like: mail.yahoo.com, hotmail.com, gmail.com or what
Thanks again
sticksnap
http://www.xblogs.org/Downloads/AppLibraries/OutlookMail.zip
SteveCS
It even allows to send text and HTML inside one message (multiple mime-parts).
The Markus
The system.net.mail class is the recomended way to handle mail sends no matter what sort of application you are building.
Sudha