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

我不是依靠主機發送電子郵件,而是考慮使用個人Gmail賬戶發送電子郵件。 這些電子郵件是發給我在演出中演奏的樂隊的個性化電子郵件。 有可能嗎? html


#1樓

這是發送帶有附件的電子郵件。 web

來源: http//coding-issues.blogspot.in/2012/11/sending-email-with-attachments-from-c.html 服務器

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

public void email_send()
{
    MailMessage mail = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
    mail.From = new MailAddress("your mail@gmail.com");
    mail.To.Add("to_mail@gmail.com");
    mail.Subject = "Test Mail - 1";
    mail.Body = "mail with attachment";

    System.Net.Mail.Attachment attachment;
    attachment = new System.Net.Mail.Attachment("c:/textfile.txt");
    mail.Attachments.Add(attachment);

    SmtpServer.Port = 587;
    SmtpServer.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");
    SmtpServer.EnableSsl = true;

    SmtpServer.Send(mail);

}

#2樓

來源在ASP.NET C#中發送電子郵件 app

如下是使用C#發送郵件的示例工做代碼,在如下示例中,我使用的是Google的smtp服務器。 網站

該代碼很容易解釋,用您的電子郵件和密碼值替換電子郵件和密碼。 google

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

#3樓

更改Gmail / Outlook.com電子郵件上的發件人: spa

爲防止欺騙-Gmail / Outlook.com不容許您從任意用戶賬戶名發送郵件。 .net

若是發件人數量有限,則能夠按照如下說明進行操做,而後將「 From字段設置爲此地址: 從其餘地址發送郵件 code

若是您想從任意電子郵件地址發送郵件(例如,用戶輸入電子郵件的網站上的反饋表,而且您不但願他們直接經過電子郵件發送給您),則能夠採起的最佳措施是: htm

msg.ReplyToList.Add(new System.Net.Mail.MailAddress(email, friendlyName));

這樣一來,您只需在電子郵件賬戶中點擊「回覆」,便可在反饋頁面上回復樂隊的支持者,但他們卻收不到您的實際電子郵件,這可能會致使大量垃圾郵件。

若是您處於受控環境中,則效果很好,可是請注意,即便指定了回覆對象,我也看到一些客戶端將電子郵件發送到發件人地址(我不知道是哪一個)。


#4樓

若是要發送後臺電子郵件,請執行如下操做

public void SendEmail(string address, string subject, string message)
 {
 Thread threadSendMails;
 threadSendMails = new Thread(delegate()
    {

      //Place your Code here 

     });
  threadSendMails.IsBackground = true;
  threadSendMails.Start();
}

並添加名稱空間

using System.Threading;

#5樓

這是一種發送郵件並從web.config獲取憑據的方法:

public static string SendEmail(string To, string Subject, string Msg, bool bodyHtml = false, bool test = false, Stream AttachmentStream = null, string AttachmentType = null, string AttachmentFileName = null)
{
    try
    {
        System.Net.Mail.MailMessage newMsg = new System.Net.Mail.MailMessage(System.Configuration.ConfigurationManager.AppSettings["mailCfg"], To, Subject, Msg);
        newMsg.BodyEncoding = System.Text.Encoding.UTF8;
        newMsg.HeadersEncoding = System.Text.Encoding.UTF8;
        newMsg.SubjectEncoding = System.Text.Encoding.UTF8;

        System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
        if (AttachmentStream != null && AttachmentType != null && AttachmentFileName != null)
        {
            System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(AttachmentStream, AttachmentFileName);
            System.Net.Mime.ContentDisposition disposition = attachment.ContentDisposition;
            disposition.FileName = AttachmentFileName;
            disposition.DispositionType = System.Net.Mime.DispositionTypeNames.Attachment;

            newMsg.Attachments.Add(attachment);
        }
        if (test)
        {
            smtpClient.PickupDirectoryLocation = "C:\\TestEmail";
            smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory;
        }
        else
        {
            //smtpClient.EnableSsl = true;
        }

        newMsg.IsBodyHtml = bodyHtml;
        smtpClient.Send(newMsg);
        return SENT_OK;
    }
    catch (Exception ex)
    {

        return "Error: " + ex.Message
             + "<br/><br/>Inner Exception: "
             + ex.InnerException;
    }

}

以及web.config中的相應部分:

<appSettings>
    <add key="mailCfg" value="yourmail@example.com"/>
</appSettings>
<system.net>
  <mailSettings>
    <smtp deliveryMethod="Network" from="yourmail@example.com">
      <network defaultCredentials="false" host="mail.exapmple.com" userName="yourmail@example.com" password="your_password" port="25"/>
    </smtp>
  </mailSettings>
</system.net>
相關文章
相關標籤/搜索