I've tried this code to send Email without entering credentials :
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp server");
mail.From = new MailAddress("sender email");
mail.To.Add("receiver email");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = CredentialCache.DefaultNetworkCredentials;
SmtpServer.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
I get this server error:
Sytem.Net.Mail.SmtpException 5.7.3 Requested action aborted;user not
authentificated
I've also tried:
SmtpServer.UseDefaultCredentials = false;
Still the same exception is thrown.
Related
My application have function to sent email trough smtp.gmail.com
It works well when i run at local, but after i publish to vps, the email not sent.
dns setting for mx(10) was pointing to smtp.gmail.com
here's the code that i use :
public ServiceResult SendEmailOrder(string Email)
{
ServiceResult result = new ServiceResult();
try
{
const string fromPassword = "password";
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
mail.From = new MailAddress("senderaddress#gmail.com");
mail.Subject = "subject mail";
mail.Body = "Body Mail";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("senderaddress#gmail.com", fromPassword);
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.Host = "smtp.gmail.com";
SmtpServer.EnableSsl = true;
mail.To.Add(new MailAddress(Email));
SmtpServer.Send(mail);
}
catch (Exception ex)
{
result.Error("Failed Send Email", ex.InnerException.Message);
}
return result;
}
is that something that i missing?
*note :
- my vps was connect to internet,
- i use google cloud before, and the code works well.
Hi guys I am doing email function for the first time and I searched up on how to do a email
try
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress("from#gmail.com");
message.To.Add(new MailAddress("to#gmail.com"));
message.Subject = "Test";
message.Body = "Content";
smtp.Port = 587;
smtp.Host = "gmail.com";
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = true;
smtp.Credentials = new NetworkCredential("from#gmail.com", "password");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
}
catch (Exception ex)
{
MessageBox.Show("err: " + ex.Message);
}
I tried running this but I am not sure what is the problem... i did not use IIS or anything
(update)
I uploaded the error image in this website
http://imgur.com/3vVZhsW
This one will Help you..! try this..!
try
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
SmtpClient smtp = new SmtpClient();
//smtp object.
Msg.From = new MailAddress("from#gmail.com");
// Recipient e-mail address.
Msg.To.Add(to#gmail.com);
//here you can pass textbox value how need to send mail.
Msg.Subject = "Please confirm your subscription";
Msg.Body = "<body></body>";
Msg.IsBodyHtml = true;
// your remote SMTP server IP.
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("from#gmail.com", "password");
smtp.EnableSsl = true;
smtp.Send(Msg);
//Msg = null;
}
catch(Exception ex)
{
Message.Show("Error:" + ex.Message);
}
void sendMail(string invoiceNumber)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("Smtp.gmail.com");
mail.From = new MailAddress("*******#gmail.com");
mail.To.Add("******#gmail.com");
mail.Subject = "Test Mail - 1";
mail.Body = invoiceNumber;
mail.Subject = "PDFs Attached";
DirectoryInfo di = new DirectoryInfo(#"C:\Users\User\Desktop\test\");
foreach (var file in di.GetFileSystemInfos("*.pdf*"))
mail.Attachments.Add(new Attachment(file.FullName));
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("*******#gmail.com", "*********");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
https://www.google.com/settings/security/lesssecureapps use this link to Allow less secure apps on of your gmail id....
void sendMail(string invoiceNumber)
{
try
{
MailMessage mail = new MailMessage();
mail.To.Add("recevermailid");
mail.From = new MailAddress("sender id");
mail.Subject = "Confirmation";
string Body = "your message body";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential
("sender email", "seneder email password");// Enter seders User name and password
smtp.EnableSsl = true;
smtp.Send(mail);
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
I'm sending email using C#. It's working fine when internet is available, but it is returning exception message again and again and stop execution when internet connection is not available. How do I ignore sending email and continue execution if connection is not available,
or any other suitable method to do that..
while (true)
{
try
{
Thread.Sleep(50);
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("mymail");
mail.To.Add("mymail");
mail.Subject = "data - 1";
mail.Body = "find attachement";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(filepath);
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("myemail", "mypassword");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
catch (Exception)
{
MessageBox.Show("Internet Connection is not found");
}
}
Any solution which depends on repeated attempts may end up looping endlessly.
Your code is sending the email synchronously, why not send asynchronously using the pickup directory
This will drop the email into the SMTP pickup directory, and the SMTP server will handle transient network issues, by retrying for a configurable period of time.
Just break out of the loop when there's no internet :
while (true)
{
try
{
Thread.Sleep(50);
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("mymail");
mail.To.Add("mymail");
mail.Subject = "data - 1";
mail.Body = "find attachement";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(filepath);
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("myemail", "mypassword");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
catch (Exception)
{
MessageBox.Show("Internet Connection is not found");
break;
}
}
It will be better to have a testconnection method and if it returns true to send the email. Something like:
while(true)
{
if(TestConnection())
{
SendEmail(); // you should leave the try...catch.. anyway in case
// the connection failed between the TestConnection
// and the SendEmail() operation. But then do not
// prompt a messagebox, just swallow
Thread.Sleep(50);
}
}
Now the TestConnection implementation, you can try to get help from the following link:
enter link description here
Try this:
while (true)
{
try
{
Thread.Sleep(50);
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("mymail");
mail.To.Add("mymail");
mail.Subject = "data - 1";
mail.Body = "find attachement";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(filepath);
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("myemail", "mypassword");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex.InnerException);
return false;
}
}
Or you can set a bool to true and then check in the end if the bool is true of false
FX:
string noInterNet = "";
while (true)
{
try
{
Thread.Sleep(50);
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("mymail");
mail.To.Add("mymail");
mail.Subject = "data - 1";
mail.Body = "find attachement";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(filepath);
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("myemail", "mypassword");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
catch (Exception ex)
{
noInterNet = ex.InnerException;
}
}
And then in the end of the code do:
if(noInterNet != "")
MessageBox.Show("Error " + noInterNet);
This code works fine on my local machine ut when I deploy it. it gives Failure sending mail error.. Please Help...
MailAddress addrsTo = new MailAddress(toEmail);
MailAddress addrsFrom = new MailAddress("XXX#XXX.com", "XXX Title");
MailMessage mailmsg = new MailMessage(addrsFrom, addrsTo);
mailmsg.Subject = mailSbjct;
mailmsg.Body = "XXX Body";
SmtpClient smtp = new SmtpClient("mail.XXX.com");
smtp.EnableSsl = false;
smtp.Port = 26;
smtp.Credentials = new NetworkCredential("XXX#XXX.com", "XXXXXXX");
try {
smtp.Send(mailmsg);
} catch (Exception exc) {
throw new XXXException(1234, "---" + exc.Message);
}
you can try this, if you are using gmail :
MailMessage mail = new MailMessage();
mail.Subject = "Your Subject";
mail.From = new MailAddress("senderMailAddress");
mail.To.Add("ReceiverMailAddress");
mail.Body = "Hello! your mail content goes here...";
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
NetworkCredential netCre = new NetworkCredential("SenderMailAddress","SenderPassword" );
smtp.Credentials = netCre;
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
}
As you have mentioned in your question works fine on my local machine.
It is suggesting that the problem is of credential which you are providing to send the mail.
Edit 1
If you are using you own domain credential then it is not going to work on the server.
The user IIS should have enough authority to send mail.
IIS User More detail
http://www.iis.net/learn/get-started/planning-for-security/understanding-built-in-user-and-group-accounts-in-iis
Here is a SO link
How to send email through IIS7?