簡體   English   中英

如何在C#中請求操作系統發送電子郵件?

[英]How to request operating system to send email in c#?

如何請求操作系統發送帶有特定主題,正文和附件的電子郵件? 操作系統應該使用默認字段啟動默認的電子郵件客戶端嗎?

具體來說,C#.net提供哪些API函數?

由於您希望Outlook發送電子郵件,因此請用Outlook的端口號和主機代替。

要使用預加載的字段打開Outlook,請執行以下操作:

using System.Diagnostics;
// ...
Process.Start(String.Format("mailto:{0}?subject={1}&cc={2}&bcc={3}&body={4}", address, subject, cc, bcc, body))

要直接發送電子郵件:

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

這是用於系統SmtpClient的簡單適配器,可以簡化您的測試。

 public class SmtpClientAdapter : ISmtpClient
    {
    private bool disposed;

    private SmtpClient smtpClient = new SmtpClient();

    public SendResult Send(MailMessage message)
    {
        if (disposed)
        {
            throw new ObjectDisposedException(GetType().FullName);
        }

        try
        {
            smtpClient.Send(message);
            return SendResult.Successful;
        }
        catch (Exception e)
        {
            return new SendResult(false, e.Message);
        }
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposed)
        {
            return;
        }

        if (disposing)
        {
            if (smtpClient != null)
            {
                smtpClient.Dispose();
                smtpClient = null;
            }
        }

        disposed = true;
    }
}

public class SendResult : SimpleResult
    {
        public SendResult(bool success, string message)
            : base(success, message)
        {
        }

        public static SendResult Successful
        {
            get
            {
                return new SendResult(true, string.Empty);
            }
        }
    }

用法

        var emailFrom = new MailAddress(sender);
        var emailTo = new MailAddress(recipient);

        var mailMessage = new MailMessage(emailFrom, emailTo)
        {
            Subject = "Subject ",
            Body = "Body"
        };

        using (var client = new SmtpClientAdapter())
        {
            return client.Send(mailMessage);
        }

這是一種簡單的方法,

public void SendEmail(string address, string subject, string message)
{
string email = "yrshaikh.mail@gmail.com";
string password = "put-your-GMAIL-password-here";

var loginInfo = new NetworkCredential(email, password);
var msg = new MailMessage();
var smtpClient = new SmtpClient("smtp.gmail.com", 587);

msg.From = new MailAddress(email);
msg.To.Add(new MailAddress(address));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;

smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = loginInfo;
smtpClient.Send(msg);
}

來源: 鏈接

沒有與此相關的操作系統API函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM