簡體   English   中英

如何使用ASP.NET和C#發送電子郵件

[英]How to send email with ASP.NET and C#

我正在嘗試創建一個接收用戶信息(包括電子郵件)的Web服務,然后發送一封確認電子郵件。 我有什么想法可以做到這一點? 我是否必須在我的機器上安裝SMTP服務器?

您可以使用任何遠程SMTP服務器,除非需要,否則無需在本地設置。 我為它創建了一個輔助方法:

您需要導入名稱空間:

using System;
using System.Net.Mail;

這是一個幫助方法,它顯示了使用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
    }
}

請注意,如果您希望郵件盡可能快地發送,請務必在完成后處置SmtpClient。 有關詳細信息,請參閱System.Net.Mail和MailMessage不立即發送消息 上面的方法已經處理它,因為SmtpClient被包裝在一個使用塊中,因此已經在這個方法中處理了。

你試過谷歌的答案嗎? 它遍布網絡和stackoverflow -

通過Gmail在.NET中發送電子郵件

關於第一個問題

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

關於smtp,沒有你可以使用谷歌的smtp例如或雅虎

通過C#通過Google Apps帳戶發送電子郵件

使用C#通過Gmail SMTP服務器發送電子郵件

為確保可靠的交付,您的解決方案應包括兩部分:

客戶端應用程序(在這種情況下是Web服務)應該將電子郵件消息(序列化為xml)提交給消息隊列(MSMQ)。

在后台運行的Windows服務應用程序(守護程序)應在郵件到達(或按計划)時檢查隊列,反序列化郵件,並將其發送到SMTP服務器。

如果這是在Windows上構建的,則可以在工作站上使用smtp4dev來表示SMTP服務器。 Windows服務,SMTP服務器,MSMQ服務器,Web服務器都可以在不同的計算機上。

不需要設置本地smtp服務器,因為上面提到的mservidio可以使用遠程服務器。 我會寫這樣的東西:

導入以下libs:

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

使用以下方法,您可以發送帶附件的郵件。

        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