我知道有很多关于这个的问题和答案,我确实阅读了分配,但它似乎已经过时了。
所以我有一个移动应用程序,用于向云服务注册用户,然后向用户的电子邮件地址发送欢迎电子邮件。
服务部分在C#WCF中完成Witch也发送邮件
以下是测试邮件的原型函数:
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
答案 0 :(得分:0)
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;
}
}
}
这是我完全正常工作的代码
答案 1 :(得分:0)
使用此代码...
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();
}