Click here to Skip to main content
15,569,547 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have developed c# programe to send email alongwith pdf file attachement. when I run the programe it producs the error "unable to read data from the transport connection an existing connection was forcibly closed by remote host". I searched help of this error on internet but could not resolve the issue. I have also on the less secure option of gmail account. Please suggest any remedy.

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
using System.Web;

namespace MailMessaje
{
    public partial class Form1 : Form
    {
               
        public Form1()
        {
            InitializeComponent();
        }
        
        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient client = new SmtpClient();
                mail.From = new MailAddress("<email>");
                mail.To.Add(this.txtToMailAddress.Text);
                mail.Subject = this.txtSubjectMail.Text;
                mail.Body = this.txtBodyMail.Text;
                System.Net.Mail.Attachment attachFile = new Attachment("D:\\exp1.pdf");
                mail.Attachments.Add(attachFile);

                client.Port = 587;
               
                client.Host = "smtp.gmail.com";
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.EnableSsl = true;
                client.Credentials = new System.Net.NetworkCredential("<email>", "<password>");
                System.Net.ServicePointManager.Expect100Continue = false;
                client.Send(mail);

                MessageBox.Show("Message Send Successfully");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.Message);
            }
        }
}}
Posted
Updated 15-Oct-18 1:31am
v4
Comments
Afzaal Ahmad Zeeshan 15-Oct-18 6:31am    
Never share your email/password on the internet. It is not a safe place to share such information. We only require the code that you are using and can try out with our own username/password if needed for a test.
F-ES Sitecore 15-Oct-18 6:43am    
This question is asked almost every day. Google for how to send email through gmail and you'll find thousands of results that will explain the issues you might face. Ultimately you shouldn't send email through gmail at all, you should use the smtp server supplied by your webhost.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900