我有一个控制台应用程序,我正在尝试从中发送邮件。
我的代码。
MailMessage message = new MailMessage(MailSender, "ToMe@me.com");
message.Subject = "Using the new SMTP client.";
message.Body = @"Using this new feature, you can send an e-mail message from an application very easily.";
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.google.com";
try
{
client.Send(message);
}
catch (Exception ex)
{
string t = ex.Message;
}
来自here
自从我得到以后,我必须遗漏一些东西:
发送邮件失败。
我在这里做错了什么?
编辑: 内在的执行。
InnerException = {"无法解析远程名称: ' smtp.google.com'"}
答案 0 :(得分:2)
您可以尝试使用
smtp.gmail.com
而不是
smtp.google.com
还要尝试确保您提供正确的凭据以及正确的端口。服务器参数信息:
<子> Source 子>
所以你可以尝试这样的事情
MailMessage message = new System.Net.Mail.MailMessage();
string fromEmail = "youremailaddress@xyz.com";
string password = "yourPassword";
string toEmail = "recipientemailaddress@abc.com";
message.From = new MailAddress(fromEmail);
message.To.Add(toEmail);
message.Subject = "Using the new SMTP client.";
message.Body = "Using this new feature, you can send an e-mail message from an application very easily.";
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
using(SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential(fromEmail, password);
smtpClient.Send(message.From.ToString(), message.To.ToString(), message.Subject, message.Body);
}
答案 1 :(得分:1)
@ Ra3IDeN ......嘿兄弟试试这个......
SmtpClient smtpClient = new SmtpClient("mail.yourwebsitename.com", 25);
smtpClient.Credentials = new System.Net.NetworkCredential("demo@yourwebsitename.com.com", "yourIdPassword");
smtpClient.UseDefaultCredentials = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
MailMessage mail = new MailMessage();
//code for: From ,CC & To
mail.From = new MailAddress("demo@yourwebsitename.com", "yourwebsite");
mail.To.Add(new MailAddress("demo@yourwebsitename.com"));
mail.CC.Add(new MailAddress("youremailid@gmail.com"));
smtpClient.Send(mail);
答案 2 :(得分:1)
如果您尝试从控制台应用程序发送电子邮件(更高级别的问题),建议您使用PostMark。原因:
发送(source)的示例代码:
var message = new PostmarkMessage()
{
To = "recipient@example.com",
From = "sender@example.com",
TrackOpens = true,
Subject = "A complex email",
TextBody = "Plain Text Body",
HtmlBody = "<html><body><img src=\"cid:embed_name.jpg\"/></body></html>",
Tag = "business-message",
Headers = new HeaderCollection{
{"X-CUSTOM-HEADER", "Header content"}
}
};
var imageContent = File.ReadAllBytes("test.jpg");
message.AddAttachment(imageContent, "test.jpg", "image/jpg", "cid:embed_name.jpg");
var client = new PostmarkClient("server token");
var sendResult = await client.SendMessageAsync(message);
答案 3 :(得分:0)
MailMessage msg = new MailMessage("YourEmail@gmail.com", "DestinationEmail@something.com");
msg.Subject = message.Subject;
msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("Message Content here as HTML", null, MediaTypeNames.Text.Html));
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", Convert.ToInt32( 587));
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("YourEmail@gmail.com", "YourPassword");
smtpClient.EnableSsl = true;
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate (object s,
System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
};
smtpClient.Credentials = credentials;
smtpClient.Send(msg);