I have a service which is suppose to send the mail to selected users,
the service looks like below
try
{
var smtpHost = System.Configuration.ConfigurationManager.AppSettings["SMTP-HOST"].ToString();//smtp.gmail.com
var fromEmail = System.Configuration.ConfigurationManager.AppSettings["SMTP-USER"].ToString();//My Id
var toEmail = "ibrahim.shaikh#chromeinfosoft.com";
var subject = "test";
var body = "body";
var smtpPort = System.Configuration.ConfigurationManager.AppSettings["SMTP-PORT"].ToString();//465
var smtpPassword = System.Configuration.ConfigurationManager.AppSettings["SMTP-PASSWORD"].ToString();//my password
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient(smtpHost);
using (SmtpClient smtp = new SmtpClient(smtpHost))
{
mail.From = new MailAddress(fromEmail);
mail.To.Add(toEmail);
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = body;
SmtpServer.EnableSsl = false;
SmtpServer.Port = Convert.ToInt32(smtpPort);
SmtpServer.Credentials = new System.Net.NetworkCredential(fromEmail, smtpPassword);
SmtpServer.Send(mail);
}
}
catch (Exception ex)
{
// Log the error
}
my config file is below
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="SMTP-HOST" value="smtp.gmail.com"/>
<add key="SMTP-PORT" value="465"/>
<add key="SMTP-USER" value="ibrahim.shaikh#chromeinfosoft.com"/>
<add key="SMTP-SSL" value="N"/>
<add key="SMTP-PASSWORD" value ="Password"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
my outlook configuration
Now I am trying to send the mail but the exception is coming.
exception: {"Failure sending mail."}
inner exception: {"Unable to read data from the transport connection: net_io_connectionclosed."} System.Exception {System.IO.IOException}
my email Id and password is correct with port details, still the same error occurred,
any suggestions what I am doing wrong here?
I am trying to send an email with Asp.Net but the email will not always be sent using the same WIFI credentials.
string email = "noreply#sender.com";
string subject = "Subject";
string MailContent = "This is the content";
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
mail.To.Add(email);
mail.From = new MailAddress("noreplay#reciever.com");
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = MailContent;
SmtpServer.Host = "smtpserver";
SmtpServer.Port = 25;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
SmtpServer.Send(mail);
}
catch (Exception ex)
{
MessageBox.Show("Exception Message: " + ex.Message);
if (ex.InnerException != null)
MessageBox.Show("Exception Inner: " + ex.InnerException);
}
I tried using this code, which was found on a different question but still can't seem to get an error.
Any alternative can work, I have a pdf which would like to be sent on an email or even one drive if possible. Thanks
This question already has answers here:
Sending email in .NET through Gmail
(26 answers)
Closed 5 years ago.
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("senttoemailaddress#gmail.com");
mail.To.Add("senttoemailaddress#gmail.com");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("emailaddress.test#gmail.com", "passW0ord");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
I'm trying to run the above code to automatically send emails, the code executes up until it reaches smtpServer.send(mail) then it just stops, the email address that I use is valid and the password is valid.
you need to do some configuration in the gmail account before you can send email via gmail
activate 2 steps authentication
enter this link and allow your email to be as SMTP server
i think this will solve the issue .
U have problem with Login\password. I've try your code and get an error:
internal class Program
{
private static void Main(string[] args)
{
try
{
var mail = new MailMessage();
var SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("senttoemailaddress#gmail.com");
mail.To.Add("senttoemailaddress#gmail.com");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new NetworkCredential("emailaddress.test#gmail.com", "passW0ord");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
Console.Write("mail Send");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
...5.5.1 Authentication Required. Learn more at...
Then i just change login\password to my and it works:
mail Send
Try with SMTP port: 465
Notice :
Google limits the amount of mail a user can send, via its portable SMTP server. This limit restricts the number of messages sent per day to 99 emails; and the restriction is automatically removed within 24 hours after the limit was reached.
I have a winforms app that sends email with code below:
MailMessage mail = new MailMessage();
mail.To.Add(textBox1.Text);
mail.Priority = MailPriority.High;
mail.From = new MailAddress("autoemailer#no-reply", "Auto Email");
mail.Subject = "Test Email";
mail.Body = "Kindly disregard if received";
SmtpClient smtp = new SmtpClient();
smtp.Host = "172.16.224.7";
smtp.Port = 25; //587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential
("username", "password"); // Sender's user name and password. Note: The username and password here is an example. Real username and password are the same as outlook's
try
{
smtp.Send(mail);
MessageBox.Show("Sent!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
I'm getting the following exception:
Insufficient system storage. The server response was: 4.3.1
Insufficient system resources
The exception message was clear that the server is low on storage/resources. But when I tried sending email thru Outlook, it sent successfully. Is there something wrong with the code? TIA for any help.
So i've been searching stackoverflow for a way to send emails using a gmail account via a asp website...
I've tried many ways including Sending email in .NET through Gmail which seemed to be the best due to amount of upvotes he got.
However sadly it still doesn't work for me! I keep getting a time out.
Here's my code:
var fromaddress = new MailAddress("from#gmail.com", "from");
var toaddress = new MailAddress("to#address.com", "to");
try
{
using (var smtpClient = new SmtpClient())
{
smtpClient.EnableSsl = true;
using (var message = new MailMessage(fromaddress, toaddress))
{
message.Subject = "Test";
message.Body = "Testing this shit!";
smtpClient.Send(message);
return true;
}
}
}
catch (Exception ex)
{
return false;
}
in my web.config I have
<system.net>
<mailSettings>
<smtp from="from#gmail.com" deliveryMethod="Network">
<network userName="from#gmail.com" password="mypassword" host="smtp.gmail.com" port="587"/>
</smtp>
</mailSettings>
</system.net>
According to several sites i've visited this should work!!! .. but it doesn't.
Is there still anything i'm doing wrong?
You never set the login add this before your smtpClient.Send() Method.
NetworkCredential NetCrd = new NetworkCredential(youracc, yourpass);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = NetCrd;
Load the web.config via ConfigurationManager if it does not work automatically.
As suggested on this page, try installing telnet to see if you can connect to the mail server. It could be a firewall issue on your server. You can also try using another port as suggested in the link.
Your code seems fine to me.
Try to deliberately enter false credentials. If you get a different errormessage you are connected to gmail and there is a problem there.
If you get the same timeout problem, it is probably not a software thing but a firewall issue.
longshot - update
Perhaps there is a web.config issue? Try to specify everything in code like this. I have this working in real life with Gmail so if this does not work it definitely is a firewall/connection thing.
SmtpClient mailClient = new SmtpClient();
//This object stores the authentication values
System.Net.NetworkCredential basicCredential =
new System.Net.NetworkCredential("username#mydomain.com", "****");
mailClient.Host = "smtp.gmail.com";
mailClient.Port = 587;
mailClient.EnableSsl = true;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = basicCredential;
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("info#mydomain.com", "Me myself and I ");
message.From = fromAddress;
//here you can set address
message.To.Add("to#you.com");
//here you can put your message details
mailClient.Send(message);
Good luck..
Can you try this out?
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress(ConfigurationManager.AppSettings["fromAddress"], ConfigurationManager.AppSettings["displayName"]); //Default from Address from config file
MailAddress toAddress = new MailAddress("abc#gmail.com", "from sender");
// You can specify the host name or ipaddress of your server
// Default in IIS will be localhost
smtpClient.Host = ConfigurationManager.AppSettings["smtpClientHost"];
//Default port will be 25
smtpClient.Port = int.Parse(ConfigurationManager.AppSettings["portNumber"]); //From config file
//From address will be given as a MailAddress Object
message.From = fromAddress;
// To address collection of MailAddress
message.To.Add(toAddress);
message.Subject = ConfigurationManager.AppSettings["Subject"]; //Subject from config file
message.IsBodyHtml = false;
message.Body = "Hello World";
smtp.DeliveryMethod = SmtpDeliveryMethod.NetWork
smtpClient.Send(message);
}
catch (Exception ex)
{
throw ex.ToString();
}
The configuration settings would be,
<configuration>
<appSettings>
<add key="smtpClientHost" value="mail.localhost.com"/> //SMTP Client host name
<add key="portNumber" value="25"/>
<add key="fromAddress" value="defaultSender#gmail.com"/>
<add key="displayName" value="Auto mail"/>
<add key="Subject" value="Auto mail Test"/>
</appSettings>
</configuration>
Put these settings EnableSSL = true and defaultCredentials="false" in your web.config settings. Gmail smtp server requires SSL set to true and mailclient.UseDefaultCredentials = false should be false if you are providing your credentials.
Update Web.Config Settings:
<mailSettings>
<smtp from="from#gmail.com" deliveryMethod="Network">
<network userName="from#gmail.com" password="mypassword" host="smtp.gmail.com" defaultCredentials="false" port="587" enableSsl="true"/>
</smtp>
</mailSettings>
And check this shorter code to send mail after providing settings in the web.config. even it send email much fast rather then specifying setting while creating the smtpclient setting in the mail sending function.
Public void SendEmail(string toEmailAddress, string mailBody)
{
try
{
MailMessage mailMessage = new System.Net.Mail.MailMessage();
mailMessage.To.Add(toEmailAddress);
mailMessage.Subject = "Mail Subjectxxxx";
mailMessage.Body = mailBody;
var smtpClient = new SmtpClient();
smtpClient.Send(mailMessage);
return "Mail send successfully";
}
catch (SmtpException ex)
{
return "Mail send failed:" + ex.Message;
}
Working very much fine at my side..
var smtpClient = new SmtpClient(gmailSmtpServer, gmailSmtpPort)
{
Credentials = new NetworkCredential(FromGEmailAddress, FromGEmailAddressPassword),
EnableSsl = true
};
try
{
using (var message = new MailMessage(fromaddress, toaddress))
{
message.Subject = "Test";
message.Body = "Testing this shit!";
smtpClient.Send(message);
return true;
}
}
catch (Exception exc)
{
// error
return false;
}