protected void Button1_Click(object sender, EventArgs e)
{
var fromAddress = new MailAddress(fromid.Text, fromname.Text);
var toAddress = new MailAddress(toid.Text, toname.Text);
string fromPassword = pswd.Text;
string subject = subjectbox.Text;
string body = bodybox.Text;
Attachment at = new Attachment(Server.MapPath("~/Penguins.jpg"));
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
Timeout = 20000,
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
IsBodyHtml = false,
})
{
message.Attachments.Add(at);
smtp.Send(message);
}
}
没有错误,但发送邮件时需要一些时间并显示“连接TimeOut”而不发送邮件...... :( 任何人都可以告诉问题在哪里。
答案 0 :(得分:4)
尝试使用带附件问题的smtp邮件的此代码
post.From = new MailAddress(From);
post.To.Add(To);
post.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
post.Subject = Subject;
post.Body = Body;
var htmlView = AlternateView.CreateAlternateViewFromString(post.Body, null, "text/html");
post.AlternateViews.Add(htmlView);
if (attachments != null && attachments.Count > 0)
{
foreach (var at in attachments)
{
post.Attachments.Add(at1);
}
}
post.IsBodyHtml = true;
//if you have relay privilege you can use only host data;
//var host = "Your SMTP Server IP Adress";
//var postman = new SmtpClient(host);
//you dont have relay privilege you must be use Network Credential
var postman = new SmtpClient("Host Server Name", Port);
NetworkCredential cred = new NetworkCredential(mail adress, password);
postman.UseDefaultCredentials = false;
postman.Credentials = cred;
postman.Send(post);
post.Dispose();
return true;
答案 1 :(得分:0)
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Net.Configuration;
MailMessage message = new MailMessage(FromMailId, ToMaiId, "YourMessage");
SmtpClient emailClient = new SmtpClient(mailsmtp.Trim());
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(FromMailId,
FromMailIdpwd);
emailClient.UseDefaultCredentials = false;
emailClient.Credentials = SMTPUserInfo;
emailClient.Send(message);
答案 2 :(得分:0)
我认为您的问题出在Gmail smtp服务器上。试试这个:
MailMessage message = new MailMessage(new MailAddress(txtSenderMail.Text, txtSenderName.Text), new MailAddress(txtToAdd.Text);
message.IsBodyHtml = true;
message.Subject = txtSubject.Text;
message.Body = txtMail.Text;
message.Priority = MailPriority.High;
SmtpClient smtp = new SmtpClient(YOUR SMTP ADDRESS, YOUR SMTP PORT);
smtp.EnableSsl = false;
smtp.UseDefaultCredentials = false; //you can use this line if you have your own SMTP server if not set it **True** (also you can get server address of your internet service company. like mine is: smtp.datak.ir but it only works on your own computer not Web server. webservers could have SMTP too.)
smtp.Send(message);