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

}

There is no error but while sending mail it takes some time and shows "Connection TimeOut" and not sending mail...:( Can any one tell where is the problem.

nwellnhof
  • 32,319
  • 7
  • 89
  • 113
Syed Yunus
  • 198
  • 3
  • 4
  • 14
  • does it work without sending Attachments? – Sleiman Jneidi May 17 '12 at 09:43
  • Hello, your problem doesn't seem to be the attachment itself but the connection to gmail server, check [this other](http://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c-sharp) stackoverflow question. – hjgraca May 17 '12 at 09:44
  • @sleiman jneidi yes it works. and mail is sent. but its not happening when i am trying with attachment. And no compile error also. – Syed Yunus May 17 '12 at 09:52
  • Sorry for saying this again but check this [link](http://stackoverflow.com/q/704636/727141), it will help. – hjgraca May 17 '12 at 09:58
  • check this link https://stackoverflow.com/questions/33665280/add-multiple-images-in-the-email-body-inlineusing-c-sharp-windows-application/49329461#49329461 – kumar chandraketu Mar 16 '18 at 21:11

3 Answers3

4

try this code for your smtp mail with attachment problem

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;
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
carlito
  • 70
  • 1
  • 5
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);
0

I think your problem is with Gmail smtp server. Try this on:

                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);
Esha
  • 391
  • 1
  • 9
  • 37
aliCna
  • 45
  • 6
  • i just tried you code and email sent to my mail but the code has exception of message sending failure. i say it again to try another SMTP server without Credentials. – aliCna May 17 '12 at 13:12