通过 Gmail 发送邮件

本文关键字:Gmail 通过 | 更新日期: 2023-09-27 18:33:26

我知道有很多关于这个问题的问题和答案,我确实读过,但它似乎都已经过时了。

因此,我有一个移动应用程序,该应用程序注册对云服务的使用,然后向用户的电子邮件地址发送欢迎电子邮件。

服务部分在 C# WCF 中完成 女巫也发送邮件

下面是用于测试邮件的原型函数:

static void SendMail()
{
    var fromAddress = new MailAddress("gmail account", "App name");
    var toAddress = new MailAddress("User email", "User account");
    const string fromPassword = "gmail password";
    const string subject = "test";
    const string body = "Hey now!!";
    var smtp = new SmtpClient
    {
        Host = "smtp.gmail.com",
        Port = 587,
        EnableSsl = true,
        DeliveryMethod = SmtpDeliveryMethod.Network,
        UseDefaultCredentials = false,
        Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
        Timeout = 20000
    };
    using (var message = new MailMessage(fromAddress, toAddress)
    {
        Subject = subject,
        Body = body
    })
    {
        smtp.Send(message);
    }
    Console.WriteLine("Sent");
    Console.ReadLine();
}

我的代码使用了其他人建议的所有内容。但我仍然收到错误消息

An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll
Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at

通过 Gmail 发送邮件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Threading;
using System.ComponentModel;
using System.IO;
using System.Net.Mime;

namespace Invoice.WCFService
{
    public class EmailSenser
    {
        public static bool SendEmail(string toMail, Stream stream, string mailBody)
        {
            bool sent = false;
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            mail.From = new MailAddress("email@gmail.com");
            mail.To.Add(toMail);
            mail.Subject = "Invoice";
            //mail.Body = "Please, see attached file";
            mail.Body = mailBody;
            mail.SubjectEncoding = System.Text.Encoding.UTF8;
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            ContentType ct = new ContentType(MediaTypeNames.Application.Pdf);
            Attachment attachment = new Attachment(stream, ct);
            ContentDisposition disposition = attachment.ContentDisposition;
            disposition.FileName = DateTime.Now.ToString("dd-MM-yyyy") + ".pdf";
            mail.Attachments.Add(attachment);
            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("email@gmail.com", "password");
            SmtpServer.EnableSsl = true;
            try
            {
                SmtpServer.Send(mail);
                sent = true;
            }
            catch (Exception sendEx)
            {
                System.Console.Write("Error: " + sendEx.Message.ToString());
                sent = false;
            }
            finally
            {
                //DBContext 
            }
            return sent;
        }
    }
}

这是我完全有效的代码

使用此代码...

    static void SendMail()
    {
        var fromAddress = new MailAddress("fromMail", "App name");
        var toAddress = new MailAddress("tomail","app");
        const string fromPassword = "passwrd";
        const string subject = "test";
        const string body = "Hey now!!";
        var smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
            Timeout = 20000,
        };
        using (var message = new MailMessage(fromAddress, toAddress))
        {
            message.Subject = subject;
            message.Body = body;
            smtp.Send(message);
        }
        Console.WriteLine("Sent");
        Console.ReadLine();
    }