Sending email using C# often ends up in spam folder - c#

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.

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.

Send email doesn't work on Server

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);

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.

How to set From Address to any email other gmail in ( Sending Email in .NET Through Gmail )?

In this post Sending Email in .NET Through Gmail we have a code to send email through gmail, in the send mail we find from Field contain gmail account that I used
I use the same code but by changing the From Address to any email I want ans set gmail address in Credentials as bellow
var fromAddress = new MailAddress("AnyEmai#mailserver.com", "From Name");
var toAddress = new MailAddress("to#example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential("from#gmail.com", fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
But in the sent email gmail account still appear in From Address and AnyEmai#mailserver.com not appear ... is there any way to do that ?
It's that way by design. You have to find another way to send outbound emails so that the return address you want shows up (I've been there, there seems to be no way to spoof the from address).
Shall you check this question change sender address when sending mail through gmail in c#
I think it is related to your inquiry.
You can import an email id in your gmail account using Mail Settings >> Accounts and Import options and that can be used for sending the mails, however if you are want to use some random email id everytime to send the mails it is not possible. Gmail will treat that as a spoofing/spam and it will reset the mail address to your original mail id before sending the mail.
using System.Net;
using System.Net.Mail;
public void email_send()
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("from#gmail.com");
mail.To.Add("to#gmail.com");
mail.Subject = "Your Subject";
mail.Body = "Body Content goes here";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("c:/file.txt");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("from#gmail.com", "mailpassword");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
There are many other mail services from which you can achieve the same but not through the gmail. Checkout the blog Send email in .NET through Gmail for sending mail using different properties.
The email address needed to be verified by gmail from the account settings.
Please find my blog post for the same describing it in detail, the steps to be followed:
http://karmic-development.blogspot.in/2013/10/send-email-from-aspnet-using-gmail-as.html
before following all the above steps, you need to authenticate your gmail account to allow access to your application and also the devices. Please check all the steps for account authentication at the following link:
http://karmic-development.blogspot.in/2013/11/allow-account-access-while-sending.html

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