Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report this article. IntroductionMost independent developers like to include a msg sending feature in their application. Well atleast I tried to.. Of course it is not a problem for a user with a smtp server access or a registered domain with an smtp server access. But unfortunately I had none of them. I use msn, gmail and yahoo for my mailing needs. Unfortunately yahoo does not offer smtp or pop access for their general/basic/free users. And msn it is not that reliable coz I was expecting a lot of mails to be sent and also to recive and as far as I know they also donot give you pop/smtp access!. Typical Example you will find in internetSo I really Hunted for a code segment that help me use my free gmail account to send E-MAIL from my .NET account. http://www.systemwebmail.com is a complete referance library and FAQ's regarding to the System.Web.Mail namespase. And my code is basically I joined different pieces together. And Came up with succes. Normally you will find thousands of sites offering the code below which has a very little use. Like MailMessage mail = new MailMessage();
mail.To = "xxxx@mailserver.com";
mail.Bcc = "yyyy@mailserver.com; user3@mailserver.net";
mail.From = "your email or name";
mail.Subject = "your subject";
mail.Body = "your subject";
SmtpMail.SmtpServer = "10.4.1.2"; //your real server goes here or
//you can add the address like "smtp.mailserver.com"
SmtpMail.Send(mail);
Well most smtp server require authentication and sometimes SSL connection so what then? The code above is USELESS. So what are the things you need to add? There you go.
How to useCopy the above methods and paste in visual studio IDE or any text editor you use. Add a referance to System.Web or System.Web.Mail dll change your account info like your mail address, password, etc and Enjoy Explanation to the codeSo what is happening on the example? See mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
//Cool. Ha? Donot Change! This part is a must if you want to access GMAIL's SMTP server This line says/declares that your smtp server requires SSL connection which is very much true/necessary for accessing to gmail's SMTP server.
step 4: assign the smtp server name in System.Web.Mail.SmtpMail class's Smtp server object like: SmtpMail.SmtpServer = "smtp.gmail.com"; you can also set the priority of your mail by
mailMsg.Priority = MailPriority.High;// MailPriority is an enum. It has High,Low, Normal option
step 5: call the send method of System.Web.Mail.SmtpMail. e.g.
System.Web.Mail.SmtpMail.Send(myMailMsg);
.NET2.0 VersionSome Changes are required for .NET v2.0 as .NET2.0 Mail is much organised and stable and so many extrafeatures. So In the Update I am including .NET2.0 version of my code.
Limitation of this codeThis code may/will not work with .NET framework v1.0. Because the MailMessage object of .NETv1.0 does not have the MailMessage.Fields Collection. This was added in .NETv1.1. So it works fine with .NETv1.1 and v2.0. conclusionThe send method does not return anything. But you can add some mechanism(like counter) to keep track of send message or even keep the return type of your method as a mailmessage type so that u can save to database for security reasons. The reason I could not provide a complete send and also recive example because I am very busy with my Thesis project. But I also wanted to publish the code sample to code project so anyone may find it helpfull. I will publish a complete article about sending and reciving mail using gmail account from your gmail account Future upgrade PlanI am planning to upload another complete article for both sending and reciveing E-Mail's of gmail account from a single application with complete source code. I need some feedback about which to choose ASP.NET code or windows programming sample or both? and I am gonna add some config file to dynamically change Mail settings like sender mail a/c, SMTP server name, default port number and priority etc. send feedback moshiur.m@gmail.com //basic authentication
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//sender email address
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "myemail@gmail.com");
//plaintext password.
//you can encrypt your password and keep in a config file when you parse just decrypt it
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "mypassword");
// port number - smtp.gmail.com use port 465 or 587. use anyone of them
//This is fully working tested code. Copy and paste the code
//Edit some parts(UserName, password) and ENJOY!
public string sendMail (string from, string to, string cc, string bcc, string subject, string body) {
// Mail initialization
MailMessage mailMsg = new MailMessage();
mailMsg.From = from;
mailMsg.To = to;
mailMsg.Cc = cc;
mailMsg.Bcc = bcc;
mailMsg.Subject = subject;
mailMsg.BodyFormat = MailFormat.Text;
mailMsg.Body = body;
mailMsg.Priority = MailPriority.High;
// Smtp configuration
SmtpMail.SmtpServer = "smtp.gmail.com";
// - smtp.gmail.com use smtp authentication
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "myemail@gmail.com");
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "mypassword");
// - smtp.gmail.com use port 465 or 587
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
// - smtp.gmail.com use STARTTLS (some call this SSL)
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
// try to send Mail
try
{
SmtpMail.Send(mailMsg);
return "";
}
catch (Exception ex)
{
return ex.Message;
}
}
|
Send E-Mail from your .NET application using your GMail Account
最新推荐文章于 2024-09-27 00:02:43 发布