locked
Use c# in asp.net to perform sending e-mail RRS feed

  • Question

  • I use asp.net in C# to build up a project. The project will upload and use in internet. I want to ask a technique which is send e-mail to client in this project.

    I think the tool is still use C# but I want to ask the sending email method ?

    Thx


    • Edited by ivanchls Friday, November 30, 2012 7:05 AM
    Friday, November 30, 2012 7:03 AM

Answers

  • Here is the sample code:

    using System.Net;
    using System.Net.Mail;
    
    var fromAddress = new MailAddress("from@gmail.com", "From Name");
    var toAddress = new MailAddress("to@example.com", "To Name");
    const string fromPassword = "fromPassword";
    const string subject = "Subject";
    const string body = "Body";
    
    var smtp = new SmtpClient
               {
                   Host = "smtp.gmail.com",
                   Port = 587,
                   EnableSsl = true,
                   DeliveryMethod = SmtpDeliveryMethod.Network,
                   UseDefaultCredentials = false,
                   Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
               };
    using (var message = new MailMessage(fromAddress, toAddress)
                         {
                             Subject = subject,
                             Body = body
                         })
    {
        smtp.Send(message);
    }

    Important note: Be sure to use System.Net.Mail, not the Deprecated System.Web.Mail. Doing SSL with System.Web.Mail is a gross mess of hacky extensions.

    Or you can refer below the post which is already marked as answered.

    http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/a75533eb-131b-4ff3-a3b2-b6df87c25cc8

    • Edited by DhavalPanchal247 Friday, November 30, 2012 9:48 AM
    • Proposed as answer by Norkk Friday, November 30, 2012 10:18 AM
    • Marked as answer by Lisa ZhuModerator Thursday, December 6, 2012 7:26 AM
    Friday, November 30, 2012 9:45 AM

All replies

  • you can use the SmtpClient.Send Method (MailMessage) method

    We use it in a MailSender Class like this :

    namespace Infrastructure.Email
    {
        using System.Collections.Generic;
        using System.IO;
        using System.Net;
        using System.Net.Mail;
        using IInfrastructure;
        using Models;
    
        public class MailSender : IMailSender
        {
            private readonly MailAddress fromAddress;
    
            private readonly IMailSettings settings;
    
            public MailSender(IMailSettings settings)
            {
                this.settings = settings;
                this.fromAddress = new MailAddress(settings.FromEmailAddress, settings.FromEmailName);
            }
    
            public void Send(MailAddress toAddress, string subject, string body)
            {
                SmtpClient smtp = this.CreateSmtpClient();
    
                using (MailMessage message = new MailMessage(this.fromAddress, toAddress) { Subject = subject, Body = body })
                {
                    smtp.Send(message);
                }
            }
    
            public void Send(MailMessageModel messageModel)
            {
                MailMessage mailMessage = new MailMessage();
                SmtpClient smtp = this.CreateSmtpClient();
    
                using (mailMessage)
                {
                    mailMessage.To.Add(
                        !string.IsNullOrEmpty(messageModel.ToUserName)
                            ? new MailAddress(messageModel.ToEmail, messageModel.ToUserName)
                            : new MailAddress(messageModel.ToEmail));
                    mailMessage.From = !string.IsNullOrEmpty(messageModel.FromUserName)
                                           ? new MailAddress(messageModel.FromEmail, messageModel.FromUserName)
                                           : new MailAddress(messageModel.FromEmail);
                    mailMessage.Subject = messageModel.Subject;
                    mailMessage.Body = messageModel.Body;
                    mailMessage.IsBodyHtml = messageModel.IsBodyHtml;
                    if (!string.IsNullOrEmpty(messageModel.Attachments))
                    {
                        List<string> attachments = new List<string>(messageModel.Attachments.Split(';'));
                        foreach (string attachment in attachments)
                        {
                            if (File.Exists(attachment))
                            {
                                mailMessage.Attachments.Add(new Attachment(attachment));
                            }
                        }
                    }
    
                    smtp.Send(mailMessage);
                }
            }
    
            private SmtpClient CreateSmtpClient()
            {
                SmtpClient smtp;
                if (this.settings.UseMailPickUpDirectory)
                {
                    // config file must have pickup directory configured
                    smtp = new SmtpClient();
                }
                else
                {
                    smtp = new SmtpClient
                        {
                            Host = this.settings.MailHost, 
                            Port = this.settings.MailHostPort, 
                            EnableSsl = this.settings.EnableSsl, 
                            DeliveryMethod = SmtpDeliveryMethod.Network, 
                            UseDefaultCredentials = this.settings.UseDefaultCredentials, 
                            Credentials = new NetworkCredential(this.settings.FromEmailAddress, this.settings.FromPassword)
                        };
                }
    
                return smtp;
            }
        }
    }


    Hope this helps,

    Here to learn and share. Please tell if an answer was helpful or not at all. This adds value to the answers and enables me to learn more.

    About me

    Friday, November 30, 2012 8:07 AM
  • Here is the sample code:

    using System.Net;
    using System.Net.Mail;
    
    var fromAddress = new MailAddress("from@gmail.com", "From Name");
    var toAddress = new MailAddress("to@example.com", "To Name");
    const string fromPassword = "fromPassword";
    const string subject = "Subject";
    const string body = "Body";
    
    var smtp = new SmtpClient
               {
                   Host = "smtp.gmail.com",
                   Port = 587,
                   EnableSsl = true,
                   DeliveryMethod = SmtpDeliveryMethod.Network,
                   UseDefaultCredentials = false,
                   Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
               };
    using (var message = new MailMessage(fromAddress, toAddress)
                         {
                             Subject = subject,
                             Body = body
                         })
    {
        smtp.Send(message);
    }

    Important note: Be sure to use System.Net.Mail, not the Deprecated System.Web.Mail. Doing SSL with System.Web.Mail is a gross mess of hacky extensions.

    Or you can refer below the post which is already marked as answered.

    http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/a75533eb-131b-4ff3-a3b2-b6df87c25cc8

    • Edited by DhavalPanchal247 Friday, November 30, 2012 9:48 AM
    • Proposed as answer by Norkk Friday, November 30, 2012 10:18 AM
    • Marked as answer by Lisa ZhuModerator Thursday, December 6, 2012 7:26 AM
    Friday, November 30, 2012 9:45 AM