SMTP Mail Sending - c#

I am using following code to send email:
MailMessage Mailer = new MailMessage();
Mailer.From = new MailAddress(From);
Mailer.To.Add(new MailAddress(To));
Mailer.Subject = Subject;
Mailer.Body = Body;
Mailer.IsBodyHtml = isBodyHTML;
SmtpClient mSmtpClient = new SmtpClient();
mSmtpClient.Host = "ExchangeServer.XXX.YYY.COM"; // Our Exchange server Name
Mailer.Attachments.Add(new System.Net.Mail.Attachment(strLogFile));
mSmtpClient.Send(Mailer);
I stopped my SMTP service but still mail was sent sucessfully. I just want to understand if my SMTP service is stopped how could program send email, shouldn't it be dumped in the mailroot folder?
Thanks,
Praveen

Looks like you've told it to use the exchange server with this line here:
mSmtpClient.Host = "ExchangeServer.XXX.YYY.COM"; // Our Exchange server Name
So it won't use your smtp service at all. Change that line of code to this:
mSmtpClient.Host = "localhost";
It will start using the local smtp service, and will fail if you try to run the code with the service stopped.

Related

Mailbox unavailable. The server response was: 5.7.54 SMTP

We are trying to send mail through SMTP setup as below
SmtpClient smtpClient = new SmtpClient(SMTPServer, SMTPPort);
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new NetworkCredential(SMTPUserName, SMTPassword);
MailAddress mailAddress = null;
if (!string.IsNullOrEmpty(sFromEmail))
mailAddress = new MailAddress(sFromEmail);
else
mailAddress = new MailAddress(SMTPEmailID);
MailMessage mailMessage = new MailMessage();
mailMessage.From = mailAddress;
mailMessage.Subject = sEmailSubject;
foreach (string toAddress in sToEmail.Split(';'))
{
if (toAddress.Trim() != string.Empty)
mailMessage.To.Add(toAddress);
}
mailMessage.IsBodyHtml = true;
mailMessage.Body = sEmailBody;
smtpClient.Send(mailMessage);
logText.Add(Environment.NewLine);
logText.Add("Mail sent successfully at " + DateTime.Now.ToString());
logCreation.createLogFileFromList(logText);
logText.Clear();
sErrorMessage = "Mail sent successfully";
but this end up with error
Mailbox unavailable. The server response was: 5.7.54 SMTP; Unable to
relay recipient in non-accepted domain
any setup I am missing here..
First
Email gateway is a SMTP server in charge of filter virus or spam. Email Gateway user maybe included spam list check this
Second
If you change your mail server ip address,your domain system may not have generalization dns in your exchange system.
Third
If you change your mail server ip address,your domain system may not have generalization your exchange system. You must be force generelization dns.
At this point can you check your project in running server
open powershell and edit your email info then run this query
send-mailmessage -to '<test#test.com>' -From '<admin#test.com>' -subject "TEST" -body "hi" -smtpServer mail.yourlocalsmtpdns.com
if you take this error change mail.yourlocalsmtpdns.com to your smtp ip address
if you not take error. You can think that your problem cause smtp domain address.
Fast solution,
you can connect to smtp with smtp ip address to your project.

Mailbox unavailable. The server response was: sorry, no mailbox here by that name (#5.1.1)

Hi I am developing web application in mvc5. I have email notifications to send it to our customers. I am using below details to send emails. I have hosted application with ssl mode. Port is 25, smtpServer is mail.ourdomain.com and email is alert#ourdomain.com. We use below code to send emails.
string AdminEmail = ConfigurationManager.AppSettings["AdminEmail"].ToString();
MailMessage mail = new MailMessage();
mail.To.Add(emailid);
mail.Bcc.Add(AdminEmail);
mail.From = new MailAddress(MailID);
mail.Subject = Subject;
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient(hostserver);
smtp.Credentials = new System.Net.NetworkCredential(MailID, Password);
smtp.Send(mail);
I found in the error log and there is Mailbox unavailable. The server response was: sorry, no mailbox here by that name (#5.1.1) May i know the root cause of this problem. Any help would be appreciated. Thank you.
This is because you have not created the object of MailAddress correctly.
Please see below way of creating mail address:
Message = new MailMessage();
Message.From = new MailAddress(UseremailId, "Your Application");
this will resolve the above issue.

Don't Send Email by hosting

I use code below to send email, but I get this error every time
Failure sending mail.
My code:
MailMessage message = new MailMessage("donot-reply#mydomain.ir", "reception#yahoo.com", "test", "msg");
message.IsBodyHtml = true;
SmtpClient emailClient = new SmtpClient("mail.mydomain.ir",110);
emailClient.Credentials = new System.Net.NetworkCredential("donot-reply#mydomain.ir", "donot-replyA!1");
emailClient.EnableSsl = true;
emailClient.Send(message);
I can send email by this email address in thunder birds, but I do not know why I can't send the email in .NET
At a quick glance, your using an invalid Simple Mail Transfer Protocol port, try the following:
Port: 25
Port: 587
Without a Stack Trace or more information on your error we won't be much use.

Can i use IP Address as SMTP host rather then smtp.email.com

i am creating an Email sending sample application, and i want to use send email from different email address like "gmail, yahoo, hotmail" so i don't want to use "smtp.email.com" as host, because if i use "smtp.email.com" as host i will have to change my host name for every different company like("smtp.gmail.com" for gmail or "smtp.mail.yahoo.com" for yahoo.com ) so
Can i use IP Address as SMTP host rather then smtp.email.com.
Please give me a solution for this so that without changing smtp host name i can use different email company to send email.
this is my code:
try
{
// setup mail message
MailMessage message = new MailMessage();
message.From = new MailAddress(textBox1.Text);
message.To.Add(new MailAddress(textBox2.Text));
message.Subject = textBox3.Text;
message.Body = richTextBox1.Text;
// setup mail client
SmtpClient mailClient = new SmtpClient("smtp.gmail.com");//here i have to change SMTP host for different email company
mailClient.Credentials = new NetworkCredential(textBox1.Text,"password");
// send message
mailClient.Send(message);
MessageBox.Show("Sent");
}
catch(Exception)
{
MessageBox.Show("Error");
}
Sure you could use IP addresses instead of names, but remember then if they ever changed the IP you're goning to stop working.. BUT.... this needs to change depending on what you are sending the mail as unless you find some form of relay proxy thats open.. AS yahoo wont recveive gmail and gmail wont receive yahoo etc.. The reality is if you are sending as that it would end up changing wether you used an IP or a name.
Your webserver however will most likely send mails from your domain, rather than your gmail/yahoo accounts.. why not send it from your domain? eg noreply#myweb.com then the smtp server remains the same as its your web provider
Of course you could do
SmtpClient mailClient
if (textbox1.Text.Contains("gmail")
{
mailClient = new SmtpClient("smtp.gmail.com");/
mailClient.Credentials = new NetworkCredential(textBox1.Text,"password");
}
else if (textbox1.Text.Contains("somemail")
{
mailClient = new SmtpClient("smtp.somemail.com");/
mailClient.Credentials = new NetworkCredential(textBox1.Text,"password");
}
etc

System.Net.Mail Server Response was 5.7.1

I am building a windows forms application for a school that has a very tight network (meaning the person I am building it for has to ask their IT services to do everything).
This application sends emails out and I am using System.Net.Mail library to do so.
SMTPServer = new SmtpClient("SMTPAddress");
MailMessage mailObj = new MailMessage("admin#xyz.com", emailAddressTo);
mailObj.IsBodyHtml = true;
mailObj.Subject = "Subject";
mailObj.Body = "<h2>Test E-Mail Message from the TSENS</h2>";
SMTPServer.Credentials = new System.Net.NetworkCredential(SMTPUserName, SMTPPassword);
SMTPServer.DeliveryMethod = SmtpDeliveryMethod.Network; //This is new code
SMTPServer.Send(mailObj);
I'm wondering if this line: SMTPServer.DeliveryMethod = SmtpDeliveryMethod.Network; will solve the latest error message he got when trying to send out an e-mail:
Is there something else that I am missing?
Just in case you are wondering the SMTPUserName and Password is his e-mail username and password that he uses to send and receive mail.
I presume that the SMTPUsername his mailadress isn't "admin#xyz.com" which is used as the sender of the mailmessage.
The mailserver seems to validate if the person doing the send is allowed to send mails on behalf of admin#xyz.com which appearantly isn't the case.
According to the SMTP specs error 5.7.1 stands for "Unable to relay" which is what you try to do.
emailClient = new SmtpClient(yourEMAILSERVER);
emailClient.Send(yourMailMessageObject); // in your case "mailObj" that you have defined already
I would avoid using SMTPServer class or set its Credentials or DeliveryMethod properties.

Categories

Resources