简体   繁体   中英

How to send email with ASP.NET and C#

I'm trying to create a web service that receives user information (including email), and then sends off a confirmation email. Any ideas how I would be able to do that? And would I have to install an SMTP server on my machine?

You can use any remote SMTP server, no need to setup one locally unless required. I created a helper method for it:

You need to import the namespaces:

using System;
using System.Net.Mail;

And here's a helper method, which shows usage of sending with SmtpClient Class :

public static void SendMessage(string smtpServer, string mailFrom, string mailFromDisplayName, string[] mailTo, string[] mailCc, string subject, string body)
{
    try
    {
        using (SmtpClient client = new SmtpClient(smtpServer))
        {
            string to = mailTo != null ? string.Join(",", mailTo) : null;
            string cc = mailCc != null ? string.Join(",", mailCc) : null;

            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(mailFrom, mailFromDisplayName);
            mail.To.Add(to);

            if (cc != null)
            {
                mail.CC.Add(cc);
            }

            mail.Subject = subject;
            mail.Body = body.Replace(Environment.NewLine, "<BR>");
            mail.IsBodyHtml = true;

            client.Send(mail);
        }
    }
    catch (Exception ex)
    {
        // exception handling
    }
}

Note that if you want the mail too send as quickly as possible without delay, you should always dispose of the SmtpClient when you're finished with it. See System.Net.Mail and MailMessage not Sending Messages Immediately for more information that. The method above is already disposing it as the SmtpClient is wrapped in a using block, so that's already taken care of in this method.

Did you try google the answer? Its all over the web and stackoverflow -

Sending email in .NET through Gmail

regarding the first question

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);

If you want the shortest way:
System.Web.Mail.SmtpMail.SmtpServer="SMTP Host Address";
System.Web.Mail.SmtpMail.Send("from","To","Subject","MessageText");

and regarding smtp , no you can use smtp of google for example or yahoo

Send Email via C# through Google Apps account

Sending email through Gmail SMTP server with C#

To ensure robust delivery, your solution should comprise 2 parts:

The client application (a web service in this case) should submit an email message (serialized as xml) to a message queue (MSMQ).

A windows service application (daemon), running in the background, should check the queue as messages arrive (or on a schedule), deserialize the message, and send it on to an SMTP server.

If this is built on windows, you can use smtp4dev on your workstation to represent the SMTP server. The Windows service, SMTP server, MSMQ server, Web server can all be on different machines.

No you don´t need to set aa local smtp server, as mservidio mentioned above you can use a remote server. I would write something like this:

import following libs:

using System.Net.Mail;
using System.Net;

And with the methods below you can send mail with attachments.

        public static void Send(string replyTo, string from, string subject, string body, bool html, List<string> files)
    {
            MailMessage message = new MailMessage();
            message.To.Add(replyTo);
            message.Subject = subject;
            message.From = new MailAddress(from);
            message.Body = body;
            message.IsBodyHtml = html;

            if (files != null) message = AttachFiles(files, message);

            SmtpClient sMail = new SmtpClient("smtp.client.com");
            sMail.Port = 25;
            sMail.DeliveryMethod = SmtpDeliveryMethod.Network;
            sMail.Credentials = new NetworkCredential("user", "pass");
            sMail.Send(message);
    }

    public static MailMessage AttachFiles(List<string> files, MailMessage message)
    {
        foreach (string filePath in files)
        {
            message.Attachments.Add(new Attachment(filePath));
        }
        return message;
    }
public void SendMail(string tomail, string password)
{
    {
        try
        {
            SmtpClient mailClient = new SmtpClient();
            MailMessage mailMessage = new MailMessage();               
            mailMessage.To.Add(tomail);
            mailMessage.From = new MailAddress("email", "show name");
            mailMessage.Subject = "Your password";
            mailMessage.Body = "Your password is :" + password;
            mailMessage.IsBodyHtml = true;
            mailMessage.Priority = MailPriority.Normal;
            mailClient.Host = "Smtp.gmail.com";
            mailClient.Port = 587;
            mailClient.UseDefaultCredentials = false;
            mailClient.Credentials = new NetworkCredential("ur email id", "ur password");
            mailClient.EnableSsl = true;
            mailClient.Send(mailMessage);
           lblPassword.Text = "<b>Mail Successfully Sent..!!</b>";
        }
        catch (Exception ex)
        {
            ex.ToString();
           lblPassword.Text = "<b>Error For Sending Mail..!!</b>";
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM