Mail sent in spam - c#

I have made asp.net webservice in C# to send mail on given mailid as parameter.
Mail is sent but it is shown as spam, not as an inbox mail.
I have used following code;
.NET CODE :
public int SendMail(string mailto, string username, string password)
{
try
{
string mailFrom = "test#gmail.com";
string siteName = "www.XYZ.com";
MailAddress fromAddress = new MailAddress(mailFrom, siteName);
SmtpClient mailClient = new SmtpClient();
MailMessage message = new MailMessage();
message.From = fromAddress;
message.To.Add(mailto);
message.Subject = "Your User Name and Password";
message.IsBodyHtml = true;
mailClient.Host = "relay-hosting.secureserver.net";
mailClient.UseDefaultCredentials = false;
mailClient.EnableSsl = false;
string body = "<HTML><BODY><CENTER><H2>Your User Name :'" + username + "' </H2><BR/><H2>Your Password :'" + password + "' </H2></CENTER></BODY></HTML>";
message.Body = body;
mailClient.Send(message);
return 1;
}
catch (Exception ex)
{
return 0;
}
}
WEB.CONFIG CODE :
<system.net>
<mailSettings>
<smtp from="test#gmail.com">
<network host="relay-hosting.secureserver.net" />
</smtp>
</mailSettings>
</system.net>
What can be the problem ?
Thank you..

The content of your MailMessage is being identified as spam by the recipient's provider or the relay server you're using is blacklisted. If you are using relay-hosting.secureserver.net the later is probably the case. I would suggest using a more trusted relay provider than GoDaddy.

Your FROM address is GMAIL.COM and you are NOT sending email from GMAIL server but from another server. Using DomainKeys Identified Mail (DKIM) and Sender Policy Framework (SPF) it is now possible (and most sysadmins do) specify authorized email sending servers for that domain in the domain's DNS records. This way, when a email recipient server receives an email, it can check for authenticity of the source of the email by checking the email sending server with the list of servers mentioned as authorized in the sender domain's DNS. If it does not match, then as per rules set or specified by the sender domain's sysadmin in their DNS record, the email may be outright rejected or saved as spam in the spam folder.
I guess this is what is happening. You should also check the sending server (SMTP) credentials using http://www.mxtoolbox.com/blacklists.aspx service before sending emails.

The email will likely show up as spam if you send from an IP address that does not have a valid SPF record for the domain you are claiming it is coming from. In this example ""relay-hosting.secureserver.net" does not have a valid IP address to act as an SMTP server for gmail.
Try doing a test sending from the actual domain you will be using in real life instead of "test#gmail.com".
If it still gets marked as spam it is very easy to add an SPF record for test#youractualdomain.com. Here is a site that has helped me in the past: http://www.zytrax.com/books/dns/ch9/spf.html

There are several things at play that conspire to raise the spam score of your email. Remember that it's not spam/notspam, but rather a "spam score" above which your email will be marked as spam by the receiving server.
I would guess that the factors at play in your case are the folowing:
HTML body with no alternative text message
From address does not correspond to actual server mail is sent from
From address does not exist
SMTP server (relay-hosting.secureserver.net) has a low reputation
No SPF or domainKey records
The solution is in several points as well:
Create an alternate text version of your message and include it in the body (this answer explains how to do that)
Use a From address that actually exists on the server the email is sent from
Use a reputable provider for your SMTP server, such as Sendgrid, postmarkApp or Mailjet
Specifying SPF and DomainKeys DNS records allows the receiving server to identify your message as coming from the right server
Good luck!

Related

Check if email exists by sending it a message

Some say it's impossible to verify that an email exists, but I also read it can be done by sending a message to it. So I want to verify if the email exists or not by sending it email.
Here is my code:
private void email_checker()
{
MailMessage mm = new MailMessage();
//The email that needs to be checked
mm.To.Add(new MailAddress(txtEmailAddress.Text, "Email Check"));
mm.From = new MailAddress("*******#gmail.com");
mm.Body = "Verified";
mm.IsBodyHtml = true;
mm.Subject = "Verification";
SmtpClient smc1 = new SmtpClient();
smc1.Host = "smtp.gmail.com";
smc1.Port = 587;
smc1.Credentials = new NetworkCredential("*****#gmail.com","********");
smc1.EnableSsl = true;
smc1.Send(mm);
}
This code is functioning, but I tried to type any random email that does not exist (it really does not exist) and the code still sends a message to it. I wanted to create a popup message that prompts whenever the message has failed to sent in that way I'll know that the email really does not exist. Or is there really no other way than sending a verification link?
There is no way to see if an email exists. Remember that your code used to send an email is asking an SmtpClient to send the email. You will only get an exception if the client's server throws one. The client's server in your case is GMail. GMail doesn't fail on sending, but replies back to the sender that the email address wasn't found. Different servers behave differently. You can setup your own server to automatically retry emails or just fail silently or throw an exception, etc. Also, servers use dns so it's possible to have an intranet mail system where the email is valid to the network, but not the internet. Another thing to keep in mind is the catch all email addresses that can be setup. So the email address in question may not be valid, but caught by the catch all and then delivered to the valid address.
So, no possible way, as of now, to validate the existence of an email address.

SMTP 5.7.57 error when trying to send email via Office 365

I'm trying to set up some code to send email via Office 365's authenticated SMTP service:
var _mailServer = new SmtpClient();
_mailServer.UseDefaultCredentials = false;
_mailServer.Credentials = new NetworkCredential("test.user#mydomain.com", "password");
_mailServer.Host = "smtp.office365.com";
_mailServer.TargetName = "STARTTLS/smtp.office365.com"; // same behaviour if this lien is removed
_mailServer.Port = 587;
_mailServer.EnableSsl = true;
var eml = new MailMessage();
eml.Sender = new MailAddress("test.user#mydomain.com");
eml.From = eml.Sender;
eml.to = new MailAddress("test.recipient#anotherdomain.com");
eml.Subject = "Test message";
eml.Body = "Test message body";
_mailServer.Send(eml);
This doesn't appear to be working, and I'm seeing an exception:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM
at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
I've tried enabling network tracing and it appears that secure communications are established (for example, I see a line in the log for the "STARTTLS" command, and later there's a line in the log "Remote certificate was verified as valid by the user.", and the following Send() and Receive() data is not readable as plain text, and doesn't appear to contain any TLS/SSH panics)
I can use the very same email address and password to log on to http://portal.office.com/ and use the Outlook email web mail to send and read email, so what might be causing the authentication to fail when sending email programmatically?
Is there any way to additionally debug the encrypted stream?
In my case after I tried all this suggestion without luck, I contacted Microsoft support, and their suggestion was to simply change the password.
This fixed my issue.
Note that the password wasn't expired, because I logged on office365 with success, however the reset solved the issue.
Lesson learned: don't trust the Office 365 password expiration date, in my case the password would be expired after 1-2 months, but it wasn't working.
This leaded me to investigate in my code and only after a lot of time I realized that the problem was in the Office365 password that was "corrupted" or "prematurely expired".
Don't forget every 3 months to "refresh" the password.
To aid in debugging, try temporarily switching to MailKit and using a code snippet such as the following:
using System;
using MailKit.Net.Smtp;
using MailKit.Security;
using MailKit;
using MimeKit;
namespace TestClient {
class Program
{
public static void Main (string[] args)
{
var message = new MimeMessage ();
message.From.Add (new MailboxAddress ("", "test.user#mydomain.com"));
message.To.Add (new MailboxAddress ("", "test.recipient#anotherdomain.com"));
message.Subject = "Test message";
message.Body = new TextPart ("plain") { Text = "This is the message body." };
using (var client = new SmtpClient (new ProtocolLogger ("smtp.log"))) {
client.Connect ("smtp.office365.com", 587, SecureSocketOptions.StartTls);
client.Authenticate ("test.user#mydomain.com", "password");
client.Send (message);
client.Disconnect (true);
}
}
}
}
This will log the entire transaction to a file called "smtp.log" which you can then read through and see where things might be going wrong.
Note that smtp.log will likely contain an AUTH LOGIN command followed by a few commands that are base64 encoded (these are your user/pass), so if you share the log, be sure to scrub those lines.
I would expect this to have the same error as you are seeing with System.Net.Mail, but it will help you see what is going on.
Assuming it fails (and I expect it will), try changing to SecureSocketOptions.None and/or try commenting out the Authenticate().
See how that changes the error you are seeing.
Be sure you're using the actual office365 email address for the account. You can find it by clicking on the profile button in Outlook365. I wrestled with authentication until I realized the email address I was trying to use for authentication wasn't the actual mailbox email account. The actual account email may have the form of: account#company.onmicrosoft.com.
We got ours working by converting the mailboxes (from address) from "shared" to "regular". Before this change, my application quit sending email when we migrated from Gmail to Office 365. No other code changes were required, besides setting the host to smtp.office365.com.
Please check below code I have tested to send email using Exchange Online:
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("YourEmail#hotmail.com", "XXXX"));
msg.From = new MailAddress("XXX#msdnofficedev.onmicrosoft.com", "XXX");
msg.Subject = "This is a Test Mail";
msg.Body = "This is a test message using Exchange OnLine";
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("XXX#msdnofficedev.onmicrosoft.com", "YourPassword");
client.Port = 587; // You can use Port 25 if 587 is blocked
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
try
{
client.Send(msg);
}
catch (Exception ex)
{
}
Port (587) was defined for message submission. Although port 587 doesn't mandate requiring STARTTLS, the use of port 587 became popular around the same time as the realisation that SSL/TLS encryption of communications between clients and servers was an important security and privacy issue.
In my case my problem was not related to the code but something to do with the Exchange mailbox. Not sure why but this solved my problem:
Go to the exchange settings for that user's mailbox and access Mail Delegation
Under Send As, remove NT AUTHORITY\SELF and then add the user's account.
This gives permissions to the user to send emails on behalf of himself. In theory NT AUTHORITY\SELF should be doing the same thing but for some reason that did not work.
Source: http://edudotnet.blogspot.com.mt/2014/02/smtp-microsoft-office-365-net-smtp.html
I got this same error while testing, using my own domain email account during development. The issue for me seemed related to the MFA (Multi Factor Authentication) that's enabled on my account. Switching to an account without MFA resolved the issue.
I had this issue since someone had enabled Security defaults in Azure.
This disables SMTP/Basic authentication. It's clearly stated in the documentation, but it's not evident by the error message, and you have to have access to the account to find out.
https://learn.microsoft.com/en-us/azure/active-directory/fundamentals/concept-fundamentals-security-defaults
It's possible to enable it per account.
https://learn.microsoft.com/en-us/exchange/clients-and-mobile-in-exchange-online/authenticated-client-smtp-submission
You need change the credentials function. Here is the substitution you need to make:
change
-*_mailServer.Credentials = new NetworkCredential("test.user#mydomain.com", "password");*
for this
-*_mailServer.Credentials = new NetworkCredential("test.user#mydomain.com", "password", "domain");*
In my case, password was expired.I just reset password and its started working again

Can SmtpClient also send email directly to the receiver?

SmptClient can be used to send an email via relay server. But is SmtpClient also able to send email directly to the receiver and not via relay server?
EDIT
Any ideas how SmtpCLient needs to be configured to be able to send emails directly to the receiver?
I tried with the following code but I got "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required."
public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
// var credentialUserName = "myAccount#gmail.com";
var sentFrom = "myAccount#gmail.com";
// var pwd = "myPwd";
System.Net.Mail.SmtpClient client =
new System.Net.Mail.SmtpClient("smtp.gmail.com");
client.Port = 587;
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
/* System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential(credentialUserName, pwd);
*/
client.EnableSsl = true;
// client.Credentials = credentials;
var mail =
new System.Net.Mail.MailMessage(sentFrom, message.Destination);
mail.Subject = message.Subject;
mail.Body = message.Body;
return client.SendMailAsync(mail);
}
}
SECOND EDIT:
Thanx, it works now. App sent email directly ( and not via myAccount#gmail.com ) to otherAccount#gmail.com. Here's the code:
public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
var sentFrom = "myAccount#gmail.com";
System.Net.Mail.SmtpClient client =
new System.Net.Mail.SmtpClient("gmail-smtp-in.l.google.com");
client.Port = 25;
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.EnableSsl = true;
var mail =
new System.Net.Mail.MailMessage(sentFrom, message.Destination);
mail.Subject = message.Subject;
mail.Body = message.Body;
return client.SendMailAsync(mail);
}
}
Thank you
Short answer: yes!
A relay server is just a server that is configured to accept all your emails and pass them on to the right destination. You can equally well contact the right destination server directly and delivery the email there.
This, of course, provided there is no firewall issues preventing you from contacting the destination server directly.
EDIT
The server smtp.gmail.com is for gmail users to send (outgoing) emails, i.e. you must authenticate with your gmail username and password in order to be allowed to send an email that way, but if you do that you can send to any recipient, i.e. also non-gmail addresses.
I understood your original question to mean you would like to send emails to (in this case) a gmail-address without using a proxy. In that case your client should behave as any arbitrary email server that is trying to send to a gmail-address, i.e. it should connect to one of official incoming SMTP servers for the domain as given by MX-records in the DNS. E.g. one of gmail's MX-records points to gmail-smtp-in.l.google.com, and if you connect to port 25 of that server you can submit an email to a gmail-address (and you can also completely spoof the sending address, but then spam-filtering might cause your email to not be delivered).
My caveat about firewall issues is to interpreted as this: most ISPs disallow outgoing TCP connections to port 25 to other hosts than their own servers. This is just because of the above mentioned spoofing possibility, i.e. if your ISP allows you to make TCP connections to port 25 of other email servers, you can use that to send spam. Therefore your ISP might not allow you to do that, and instead you should relay your emails via your ISP so they can take appropriate measures if you try to spam people.

Permission error when sending e-mail using my SMTP server in IIS7

I just recently bought my own server with IIS7, and I'm trying to set up SMTP so that I can send an e-mail from my website.
Here's my smtp settings:
Here is my code that sends the e-mail:
private static void SendEmail(IEnumerable<MailAddress> to,
IEnumerable<MailAddress> bcc, MailAddress from,
string subject, string bodyHtml)
{
var mail = new MailMessage { From = from, Subject = subject,
Body = bodyHtml, IsBodyHtml = true };
foreach (var address in to)
{
mail.To.Add(address);
}
foreach (var address in bcc)
{
mail.Bcc.Add(address);
}
try
{
string server = ConfigurationManager.AppSettings["SMTPServer"];
int port = Int32.Parse(ConfigurationManager.AppSettings["SMTPPort"]);
var smtp = new SmtpClient
{
Host = server,
Port = port
};
smtp.Send(mail);
}
catch (Exception err)
{
}
}
And my config settings:
<add key="SMTPServer" value="localhost" />
<add key="SMTPPort" value="25" />
I get an error at smtp.Send(mail); that says:
Bad sequence of commands. The server response was: This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server.
Well, I have no authentication requirements on my smtp server, it says so in my settings in the screenshot.
I looked around, and other people had this problem if they were sending the e-mail from a different e-mail specified in their settings, but I'm sending mine from info#mysite.com. I am sending it to an #gmail.com account though, so it is sending to a non-local e-mail address.
What am I doing wrong here?
I wanted to add the answer to this for others searching
Make sure SMTP is up and running and no errors are being thrown. Here is a reference that might help.
http://forums.iis.net/p/1157046/1901343.aspx
I met more than 2 people when I wanted to send.
I'm just sending it to 2 people.
My problem is solved.

Using System.Net.Mail.SmtpClient, change the SMTP FROM email address

As it says in the title, I wish to change the FROM address provided to the SMTP server, as opposed to the FROM address in the email envelope.
The closest sample I can find is from Java, which is can be found here
Thanks
Bottom line is, you can't do this. The FROM address used in System.Net.Mail is used for both the SMTP transaction (Envelope-From) and the MailMessage from header value.
Sorry,
Dave
The FROM provided to the SMTP server is the login of the SmtpClient while the one in the Mail is the FROM in the MailMessage.
SmtpClient smtp = new SmtpClient();
smtp.Host = "myserver.address.com";
smtp.Credentials = new NetworkCredential("me#server.com", "myPassword");
MailMessage msg = new MailMessage();
msg.From = "otherMe#server.com";
//OTHER MESSAGE SETTINGS
smtp.Send(msg);
This should send an e-mail from "otherMe#server.com" using the authentication on the server for the user "me#server.com"
The Java example is about return address, not From.
As Far as I know, you can't do this. SMTP servers use the From address to decide if the want to relay or not.
The only other credential you've got is the Login to the SMTP server.
Unfortunately, this is not possible.
First there is a syntax error for smtp.Host = smtp.serv.com; This is not valid written string type, and the second thing is that the property Host doesn't exist.
As explained by bzlm, if you're using Network delivery method of SmtpClient, then you can set MAIL FROM by setting the MailMessage.Sender property. Note, however, that this will have the side-effect of adding a Sender heady to your message, which will cause many email clients to display present the message sender as "X on behalf of Y".

Categories