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

}

エラーはありませんが、メールの送信中に時間がかかり、「接続タイムアウト」が表示され、メールが送信されません...:(どこに問題があるのか​​ わかりますか。

4

3 に答える 3

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;
于 2012-11-30T08:28:02.063 に答える
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);
于 2012-05-17T09:44:00.337 に答える
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);
于 2012-05-17T09:55:27.790 に答える