Sunday, August 7, 2011

ASP net mail send simple method

to is email@example.com
subj is hi or hello
obj is html or string

WebSites.SystemEmailSetting.Email бол зохиомол class ба myemail@gmail.com
чухал хэсэг нь доорх мөрөнд бий. gmail.com ийн өөрийнхөө account ийг ашиглан майл илгээхийн тулд нууц үгээ энд оруулна.Timeout aa шалгаад send. Миний жишээнд smtp.gmail.com майл гаргах порт нь 587 байна.

 new System.Net.NetworkCredential(WebSites.SystemEmailSetting.Email, WebSites.SystemEmailSetting.Password);

public static bool systemMailSend(string to, string subj, string obj)
        {
            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
            mail.To.Add(new System.Net.Mail.MailAddress(to));
            mail.From = new System.Net.Mail.MailAddress(WebSites.SystemEmailSetting.Email);
            mail.Subject = subj;
            mail.Body = obj;
            mail.IsBodyHtml = true;

            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
            client.Port = WebSites.SystemEmailSetting.Port;//587;//465;
            client.Host = WebSites.SystemEmailSetting.Host;//"smtp.gmail.com";//"smtp.googlemail.com";
            client.EnableSsl = true;
            client.Credentials = new System.Net.NetworkCredential(WebSites.SystemEmailSetting.Email, WebSites.SystemEmailSetting.Password);
            client.Timeout = 600000;//10min
            client.Send(mail);

            return true;
        }

No comments: