I'm using the following basic code:
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.to.add("someone#hotmail.com");
msg.to.add("someone#gmail.com");
msg.to.add("someone#myDomain.com");
msg.From = new MailAddress("me#myDomain.com", "myDomain", System.Text.Encoding.UTF8);
msg.Subject = "subject";
msg.SubjectEncoding = System.Text.Encoding.UTF8;
msg.Body = "body";
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml = false;
//Add the Creddentials
SmtpClient client = new SmtpClient();
client.Host = "192.168.0.24";
client.Credentials = new System.Net.NetworkCredential("me#myDomain.com", "password");
client.Port = 25;
try
{
client.Send(msg);
}
catch (System.Net.Mail.SmtpException ex)
{
sw.WriteLine(string.Format("ERROR MAIL: {0}. Inner exception: {1}", ex.Message, ex.InnerException.Message));
}
Problem is the mail is only sent to the address in my domain (someone#mydomain.com) and I get the following exception for the 2 other addresses:
System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: No such domain at this location
I suspect it's something to do with something blocking my smtp client but not sure how to approach this.
Any idea? thanks!
Ron is correct,just use the 587 port and it will work as u wish.
Check this code and see if it works:
using System;
using System.Windows.Forms;
using System.Net.Mail;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your_email_address#gmail.com");
mail.To.Add("to_address#mfc.ae");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
Try using port=587. Here's useful related link: http://mostlygeek.com/tech/smtp-on-port-587/comment-page-1/
Related
I have the following code I use to send emails from my application:
var config = DeserializeUserConfig(perfilAcesso.GetClientConfigPath() + "Encrypted");
using (SmtpClient client = new SmtpClient())
{
client.Host = config.GetClientSMTP();
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(config.GetClientEmail(), config.GetClientPassword());
using (MailMessage mail = new MailMessage())
{
mail.Sender = new MailAddress(config.GetClientEmail(), config.GetClientName());
mail.From = new MailAddress(config.GetClientEmail(), config.GetClientCompany());
mail.To.Add(new MailAddress("emailToReceive"));
mail.Subject = "[PME] SOS - Equipamento Parado";
mail.Body = "";
client.Send(mail);
MessageBox.Show("Email enviado com sucesso!");
}
}
I have set up three possible SMTP hosts for the user to choose from: Gmail ("smtp.gmail.com"), Outlook ("smtp.live.com") and Yahoo ("smtp.mail.yahoo.com").
When I try to send and email using a Yahoo account, this exception is thrown:
System.Net.Mail.SmtpException: Mailbox unavailable. The server response was: Requested mail action not taken: mailbox unavailable.
I know for a fact that when sending emails with Gmail and Outlook accounts, the method works perfectly, because I tried it several times.
What am I doing wrong? Any help will be greatly appreciated!
Step 1
client.Port = 587;
Step 2
go to https://login.yahoo.com/account/security
Step 3
enable Allow apps that use less secure sign-in
Step 4 : full code
using System;
using System.Net.Mail;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
using (SmtpClient client = new SmtpClient())
{
client.Host = config.GetClientSMTP();
client.EnableSsl = true;
client.Port = 587;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(config.GetClientEmail(), config.GetClientPassword());
using (MailMessage mail = new MailMessage())
{
mail.Sender = new MailAddress(config.GetClientEmail(), config.GetClientName());
mail.From = new MailAddress(config.GetClientEmail(), config.GetClientCompany());
mail.To.Add(new MailAddress(config.emailToReceive));
mail.Subject = "Test 2";
mail.Body = "Test 2";
var isSend = false;
try
{
client.Send(mail);
isSend = true;
}
catch (Exception ex)
{
isSend = false;
Console.WriteLine(ex.Message);
}
Console.WriteLine(isSend ? "All Greeen" : "Bad Day");
Console.ReadLine();
}
}
}
}
}
if you add the same emails
mail.To.Add(new MailAddress(config.emailToReceive));
mail.To.Add(new MailAddress(config.emailToReceive));
you will git Error
Bad sequence of commands. The server response was: 5.5.0 Recipient already specified
if you want to reuse MailMessage
mail.To.Clear();
Are you sure that your from/to addresses are correct?
From and sender have to be your Yahoo addresses.
Here's a sample that works:
public static void Main(string[] args)
{
using (SmtpClient client = new SmtpClient())
{
client.Host = "smtp.mail.yahoo.com";
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("my-yahoo-login", "yahoo-password");
using (MailMessage mail = new MailMessage())
{
// This works
mail.Sender = new MailAddress("my-email-address#yahoo.co.uk", "Tom Test");
mail.From = new MailAddress("my-email-address#yahoo.co.uk", "Tom Test");
mail.To.Add(new MailAddress("my-email-address#outlook.com"));
/* This does not
mail.Sender = new MailAddress("my-email-address#outlook.com", "Tom Test");
mail.From = new MailAddress("my-email-address#outlook.com", "Tom Test");
mail.To.Add(new MailAddress("my-email-address#yahoo.co.uk"));
*/
mail.Subject = "Test mail";
mail.Body = "Test mail";
client.Send(mail);
Console.WriteLine("Mail sent");
}
}
}
If you put your non-Yahoo address in Sender and From fields (the commented code) you'll get the same exception.
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.
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);
I'm just wondering if i have the correct code to check if a proxy supports SMTP or not. I'm not a spammer or anything it just randomly interested me. I Have this for testing:
//host = proxy IP address
public static void can_mail(string host)
{
MailMessage msg = new MailMessage();
msg.Body = "Email is working!";
msg.From = new MailAddress("me#whatever.com");
msg.IsBodyHtml = false;
msg.Subject = "Mail Test";
msg.To.Add(new MailAddress("myemailaddress#myserver.com"));
try
{
SmtpClient client = new SmtpClient();
client.Host = host;
client.Port = 25;
client.EnableSsl = false;
client.Send(msg);
Console.WriteLine("{0} connected, mail probably works?", host);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.Read();
}
Have I missed something here?
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?