Send email doesn't work on Server - c#

I use Belowe code to Send Email .It's work fine when I test it on local but when I upload site on the Server and test it ,it isn't send Email .
I don't know why dont work and search on Internet but I can't found any similar Answer.
MailMessage message = new MailMessage("info#mydomain.ir", "xxx#yahoo.com", "Subject", Message);
message.IsBodyHtml = true;
SmtpClient emailClient = new SmtpClient("mail.mydomain.ir",587);
emailClient.Credentials = new System.Net.NetworkCredential("info#mydomain.ir", "psw");
emailClient.Send(message);

Related

Sending email using C# often ends up in spam folder

I am sending email from my outlook email using MailKit package of C#.
However, for same email body and subject when I send it using Outlook app, it ends up in recipient inbox. But when I send it using my program it ends up in spam folder. Here is my code sample:
var message = new MimeMessage();
message.From.Add(new MailboxAddress("Sender Name", "sender#example.com"));
message.To.Add(new MailboxAddress("Recipient Name", "recipient#example.com"));
message.Subject = "Test Email";
message.Body = new TextPart("plain")
{
Text = "This is a test email sent using MailKit."
};
message.Headers.Add("X-Priority", "2");
message.Headers.Add("X-MSMail-Priority", "Normal");
// create the SMTP client
using (var client = new MailKit.Net.Smtp.SmtpClient())
{
// connect to the SMTP server
client.Connect("outlook.office365.com", 587, false);
// authenticate with the SMTP server
client.Authenticate("sender#example.com", "password");
// send the message
client.Send(message);
// disconnect from the SMTP server
client.Disconnect(true);
}
I am not sure why it is behaving different when I send mail using this code . Help me how can I improve the code to not ending up in spam or let me know if there any other solution.

Failed to send an EMail with body contains ip address and port no

I have create function to send an email. This function was work successful on localhost but on server its failed without any exception. I know the problem comes from my Port on IP Address.
The sample body is string body = "<p>Please click here</p>Thank You."
The problem is : between IP Address and Port.
Successful send an email if i remove :.
Do you guys have any ideas?
public void Sent(string sender, string receiver, string subject, string body)
{
using (MailMessage mail = new MailMessage(sender, receiver))
{
using (SmtpClient client = new SmtpClient())
{
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "mail.companyName.com.my";
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = body;
client.Send(mail);
}
}
}
You are doing it right, the code to send the mail is ok (you may want to revise the function name and make the smtp host name configurable, but that is not the point here).
The e-mail delivery fails on a relay, there is no immedieate feedback (no exception) to the client about this kind of failure.
The best bet is the IncreaseScoreWithRedirectToOtherPort property set in Set-HostedContentFilterPolicy in case your mail provider is Office365, or a similar spam filter mechanism in any other mail provider that is encountered down the mail delivery chain.
You can set a reply-to address and hope that the destination server will bounce a delivery failure that gives you more information. Or have the admin of the mail server look up the logs. More information here:
https://serverfault.com/questions/659861/office-365-exchange-online-any-way-to-block-false-url-spam
Try setting the 'mail.Body' to receive a Raw Html message instead of a encoded string, like:
mail.Body = new System.Web.Mvc.HtmlHelper(new System.Web.Mvc.ViewContext(), new System.Web.Mvc.ViewPage()).Raw(body).ToString();
Or put a using System.Web.Mvc at the beginning so it gets shorter and easier to understand:
using System.Web.Mvc
mail.Body = new HtmlHelper(new ViewContext(), new ViewPage()).Raw(body).ToString();

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.

SMTP Mail Sending

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.

Categories

Resources