2

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?

6 Answers 6

8

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.

Sign up to request clarification or add additional context in comments.

3 Comments

For reference, the using block will automatically dispose client when you're done with it. No need to dispose it manually.
Yes, good point, it's already being disposed in the method above.
Additionally, the SmtpClient class doesn't implement the IDisposable interface prior to .Net 4, so there is no Dispose method available and the using block also won't work on earlier .Net versions.
2

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

Sending email in .NET through Gmail

Comments

1

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#

3 Comments

Note that System.Web.Mail is deprecated now. msdn.microsoft.com/en-us/library/system.web.mail.aspx
this is the updated for the System.Net.Mail.Smtpmail Sending email through System.Net.Mail.Smtpmail in asp.net 3.5/2.0 , I updated my answer
looks like with the google servers we can only send from an @gmail account. any ideas on how to send it from our own domain?
0

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.

Comments

0

No you don´t need to set a a 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;
    }

Comments

0
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>";
        }
    }
}

1 Comment

You didn't address his other question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.