MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient(ConfigurationManager.AppSettings["Host"].ToString());//smtp.gmail.com mail.From = new MailAddress(ConfigurationManager.AppSettings["MailFrom"].ToString());//[email protected]
mail.To.Add(MailTo); // Sending MailTo
List li = new List(); li.Add("[email protected]"); //mail.CC.Add(string.Join(",", li)); // Sending CC //mail.Bcc.Add(string.Join(",", li)); // Sending Bcc mail.Subject = MailSubject; // Mail Subject //mail.Body = "TwoWaySummary Report *** This is an automatically generated email, please do not reply ***";
System.Net.Mail.Attachment attachment; attachment = new System.Net.Mail.Attachment(file); //Attaching File to Mail mail.Attachments.Add(attachment);
// SmtpServer.Port = Convert.ToInt32(ConfigurationManager.AppSettings["PORT"]); //PORT SmtpServer.Port = 465;//also tried with 587 SmtpServer.EnableSsl = true; SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network; SmtpServer.UseDefaultCredentials = false; //SmtpServer.Credentials = new NetworkCredential("Email id of Gmail", "Password of Gmail"); // SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "shweta123"); SmtpServer.Credentials = new NetworkCredential("[email protected]", "Rsl@2005"); // SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "shweta123"); SmtpServer.Send(mail);
Shweta Deshmukh34 reputationPosted Aug 27, 2019, 2:13 AM EDT
0
I am getting data from database exporting in excel and sending the excel file through email..
but unable to send the file. it is showing err: Failure sending mail(inner Exception :unable to read data from the transport connection net_io_connectionclosed. gmail). please help me solve the issue.. would be a great help..
Thanks in Advance sir
0
Mageshwaran RRank #411 · 6.1K reputationPosted Nov 18, 2019, 7:04 PM EST
0
Hi,
Try this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
namespace ConsoleApplication2
{
class Program
{
staticvoid Main(string[] args)
{
var client = new SmtpClient("smtp.gmail.com", 587)
Chandu KumawatRank #1,449 · 1.1K reputationPosted Apr 16, 2016, 4:11 AM EDT
Amit KumarRank #459 · 5.4K reputationPosted Apr 16, 2016, 5:16 AM EDT
Abhishek KumarRank #1,735 · 872 reputationPosted Apr 16, 2016, 6:45 AM EDT
Amit KumarRank #459 · 5.4K reputationPosted Apr 16, 2016, 6:55 AM EDT
Rajeesh MenothRank #118 · 18.4K reputationPosted Apr 17, 2016, 6:08 AM EDT
Abhishek KumarRank #1,735 · 872 reputationPosted Apr 21, 2016, 7:17 AM EDT
Rajeesh MenothRank #118 · 18.4K reputationPosted Apr 21, 2016, 9:04 PM EDT
Shweta Deshmukh34 reputationPosted Aug 27, 2019, 2:06 AM EDT
{
try
{
string AppLocation = "";
AppLocation = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
AppLocation = AppLocation.Replace("file:\\", "");
string file = AppLocation + "\\ExcelFiles\\DataFile.xlsx";
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient(ConfigurationManager.AppSettings["Host"].ToString());//smtp.gmail.com
mail.From = new MailAddress(ConfigurationManager.AppSettings["MailFrom"].ToString());//[email protected]
mail.To.Add(MailTo); // Sending MailTo
List
li.Add("[email protected]");
//mail.CC.Add(string.Join
//mail.Bcc.Add(string.Join
mail.Subject = MailSubject; // Mail Subject
//mail.Body = "TwoWaySummary Report *** This is an automatically generated email, please do not reply ***";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(file); //Attaching File to Mail
mail.Attachments.Add(attachment);
// SmtpServer.Port = Convert.ToInt32(ConfigurationManager.AppSettings["PORT"]); //PORT
SmtpServer.Port = 465;//also tried with 587
SmtpServer.EnableSsl = true;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.UseDefaultCredentials = false;
//SmtpServer.Credentials = new NetworkCredential("Email id of Gmail", "Password of Gmail");
// SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "shweta123");
SmtpServer.Credentials = new NetworkCredential("[email protected]", "Rsl@2005");
// SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "shweta123");
SmtpServer.Send(mail);
}
catch (Exception ex)
{
logger(ex.Message);
logger(ex.StackTrace);
}
finally
{
}
}
Shweta Deshmukh34 reputationPosted Aug 27, 2019, 2:13 AM EDT
Mageshwaran RRank #411 · 6.1K reputationPosted Nov 18, 2019, 7:04 PM EST
Arun Kumar TiwariRank #3,157 · 454 reputationPosted Nov 18, 2019, 10:11 PM EST
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts