简体   繁体   English

如何使用ASP.NET和C#发送电子邮件

[英]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. 我正在尝试创建一个接收用户信息(包括电子邮件)的Web服务,然后发送一封确认电子邮件。 Any ideas how I would be able to do that? 我有什么想法可以做到这一点? And would I have to install an SMTP server on my machine? 我是否必须在我的机器上安装SMTP服务器?

You can use any remote SMTP server, no need to setup one locally unless required. 您可以使用任何远程SMTP服务器,除非需要,否则无需在本地设置。 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 : 这是一个帮助方法,它显示了使用SmtpClient类发送的用法:

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. 请注意,如果您希望邮件尽可能快地发送,请务必在完成后处置SmtpClient。 See System.Net.Mail and MailMessage not Sending Messages Immediately for more information that. 有关详细信息,请参阅System.Net.Mail和MailMessage不立即发送消息 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. 上面的方法已经处理它,因为SmtpClient被包装在一个使用块中,因此已经在这个方法中处理了。

Did you try google the answer? 你试过谷歌的答案吗? Its all over the web and stackoverflow - 它遍布网络和stackoverflow -

Sending email in .NET through Gmail 通过Gmail在.NET中发送电子邮件

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 关于smtp,没有你可以使用谷歌的smtp例如或雅虎

Send Email via C# through Google Apps account 通过C#通过Google Apps帐户发送电子邮件

Sending email through Gmail SMTP server with C# 使用C#通过Gmail SMTP服务器发送电子邮件

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). 客户端应用程序(在这种情况下是Web服务)应该将电子邮件消息(序列化为xml)提交给消息队列(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. 在后台运行的Windows服务应用程序(守护程序)应在邮件到达(或按计划)时检查队列,反序列化邮件,并将其发送到SMTP服务器。

If this is built on windows, you can use smtp4dev on your workstation to represent the SMTP server. 如果这是在Windows上构建的,则可以在工作站上使用smtp4dev来表示SMTP服务器。 The Windows service, SMTP server, MSMQ server, Web server can all be on different machines. Windows服务,SMTP服务器,MSMQ服务器,Web服务器都可以在不同的计算机上。

No you don´t need to set aa local smtp server, as mservidio mentioned above you can use a remote server. 不需要设置本地smtp服务器,因为上面提到的mservidio可以使用远程服务器。 I would write something like this: 我会写这样的东西:

import following libs: 导入以下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>";
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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