Sending Mail with an attachment using SMTP

14,283

Solution 1

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

Solution 2

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;
Share:
14,283

Related videos on Youtube

Syed Yunus
Author by

Syed Yunus

Working as .net developer in dWise solution and service Pvt. Ltd., Bangalore.

Updated on June 04, 2022

Comments

  • Syed Yunus
    Syed Yunus almost 2 years
    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.

    • Sleiman Jneidi
      Sleiman Jneidi almost 12 years
      does it work without sending Attachments?
    • hjgraca
      hjgraca almost 12 years
      Hello, your problem doesn't seem to be the attachment itself but the connection to gmail server, check this other stackoverflow question.
    • Syed Yunus
      Syed Yunus almost 12 years
      @sleiman jneidi yes it works. and mail is sent. but its not happening when i am trying with attachment. And no compile error also.
    • hjgraca
      hjgraca almost 12 years
      Sorry for saying this again but check this link, it will help.
    • kumar chandraketu
      kumar chandraketu about 6 years
  • aliCna
    aliCna almost 12 years
    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.