change sender address when sending mail through gmail in c# - c#

I have used the following code to send mail from my web application using a gmail account. My question is, can i change the sender address to another address other than original sender(gmail) address?
My code is as follows:
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("sample#gmail.com", "*******");
Whatever i do is useless as i always receive mail from sample#gmail.com. Is it possible to change it?
I have changed to
mail.From = new System.Net.Mail.MailAddress("sample#yahoo.com"); but i received the mail with the from address sample#gmail.com and not from the new "From" address. I think gmail smtp overwrites the from address with the original credential.

This is the solution:
use the codes above to set mail.From = new MailAddress(address, display name)
in Gmail, go to Mail Settings >> Accounts and Import.
Add the email account you will use as sender in "Send Mail As". (tick as Alias)
This works for me

Gmail doesn't allow you to change the FROM to something different than your gmail account.
It doesn't matter what you use, they over-write it, before they relay it on. This prevent spamming/spoofing.

Yes just use the From property of the MailMessage
eg.
mail.From = "newemail#email.com";
EDIT: Also, see this post for more detailed info on how to emails via gmail in C#
Sending email in .NET through Gmail
EDIT: Although this works for mail in general, it appears this won't work for gmail as google overwrite it before its sent (see #Dave wanta's answer)

If you have a limited number of senders you can do as #philip suggested. For instance you may have customerservice#example.com, simon#example.com and philip#example.com or even alias#example.com. As long as they are approved senders on the actual gmail.com website you can send from them.
Gmail.com : Sending mail from a different address
If you are expecting to send from an arbitrary user (such as a customer service form on a website where the user enters their email and you don't want them emailing you directly) about the best you can do is this :
msg.ReplyToList.Add(new System.Net.Mail.MailAddress(email, friendlyName));
If you're in a controlled environment this works great, but please note that I've seen some email clients send to the from address even when reply-to is specified (I don't know which).

Check #56 and #58. They might be relevant to what you want to do
https://code.google.com/p/google-apps-script-issues/issues/detail?id=172

Related

How to send an e-mail from *any* email address in ASP.NET?

I have a basic SmtpClient and I use it to send e-mails.
using (var client = new SmtpClient())
using (var mail = new MailMessage())
{
mail.From = new MailAddress(fromEmailAddress);
mail.Body = "body text";
mail.To.Add(new MailAddress(myEmailAddress));
client.Send(mail);
}
This code works well if I set the fromEmailAddress to a real e-mail address. If I set it to a fake e-mail address, let's say test#test.com, the code sends the message, but I don't receive the e-mail.
I'm using it on a contact form where there is a field for their e-mail and a field for their message.
Is there a way to send the e-mail regardless of whether the e-mail is valid or not? (In PHP's mail method, it was possible)
Optionally, is it possible to check if the e-mail had been received by me? (to display an error to the user)
You make a mistake in thinking your end matters.
What matters is how stupid or not - and in these days you can count on not - the receiving email server is in regarding to spam.
Your email is simply thrown away for being spam, and it is likely obvious - like the domain you send "from" (fake address) either not existing or having an SPF or other record listing the allowed senders - which you aren't one of.
Simple like that. The internet is a bad place and every sysadmin not a total idiot deploys anti spam mechanisms.
Now, if you put up some form like that or something, it needs to be fixed. Seen that many times. You can not do that - because domains are protected. Which means you can not just pretend you are allowed to send on another domain's behalf. I have seen this type sometimes with contact forms - it's a mistake of programmers not understanding that my domains have SPF records.
C# aside, thats not a good idea if you can avoid it. Setting the 'From' field doesnt actually send from that address - this would mean that anyone could impersonate your bank, for instance.
Most mail providers will regard it quite poorly in terms of spam score - meaning you are likely to have delivery issues if you are sending from a domain that you are not authorised to send from - and that is at least one possible reason why you are experiencing issues with fake addresses.
Here's some more info on why this is bad (see point #4), and some other useful tips - https://www.campaignmonitor.com/blog/email-marketing/2015/09/9-things-that-are-killing-your-email-deliverability/

Sending mail from office365 account C# mail headers

I can send mail with a valid Office365 account using my C# web application to a list of opt-in addresses. So it is our own data source with our customers in it. The problem is that it is probably not received by our customers. I have send a mail from the info account to my own office365 account and pasted the headers in mxtoolbox and got a message that my local pc is blacklisted. even when it is using the smtp of office365. We have dkim enabled and spf in the dns. What else should I do to troubleshoot this?
Here is my simple C# code for sending the mail (which works, but might needs additional anti-spam stuff?)
SmtpClient sc = new SmtpClient();
sc.Host = "smtp.office365.com";
sc.Credentials = new System.Net.NetworkCredential(SmtpUsername, SmtpPassword);
sc.Port = 587;
sc.EnableSsl = true;
Should I add my home IP and from the office to the SPF dns entry, even when I am using the smtp of office365?
this is the value for Authentication-Results
dkim=none (message not signed) header.d=none;ouroffice365domain.com; dmarc=none action=none
Is that the cause for getting on the blacklist? Please note that I am not trying to send spam. It is a valid data source where people opted in for. We are just migrated to office365 and need to send a mailing to our customers from our custom web application. We are aware of the limitations (10k per day) and 1 mail every 2 sec.
edit will change this: https://stackoverflow.com/a/23409351/169714 because I just used the mail address and not the first and last name of the customer.
Not having DKIM setup is not going to get you blackisted, having it set up improperly can. Your HOME PC should have nothing to do with SPF, it's the lasting sending IP which should be OFFICE 365 IP. Did you validate your DKIM and SPF by sending an email to mailtest#unlocktheinbox.com or check-auth#verifier.port25.com?
I also don't think your LOCAL PC is blacklisted, it's the IP of your INTERNET Provider that is blacklisted. Unless you have a static IP it will change.

Email not reaching a specific email domain from php or C#

I am currently using an Office 365 account to send email from various websites. I have implemented an smtp client in both php and C# to send the mail. I use GoDaddy and Rackspace to hosts my sites.
The mail reaches most address without issue, however, I have a business domain through Google, which ends with .co, and emails do not make it to my inbox there.
Here are the settings for the PHPMailer client:
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPSecure = "tls";
$mail->Host = 'smtp.office365.com';
$mail->SMTPAuth = true;
$mail->Username = '<myuser>';
$mail->Password = '<mypassword>';
$mail->Port = 587;
$mail->setFrom('noreply#<mymaildomain>.com', 'MyMail');
The settings send email find to yahoo, gmail, etc. However, when I try to send it to my .co email address, the client indicates the mail was sent, but it never shows up in the inbox.
I am wondering if it is an issue with a domain in Google or if the .co is the problem. Is there any special setting I need to make on the client for .co addresses?
UPDATE
I changed phpmailer to simply use php's mail function. The mail was sent through to my .co domain, but I still have no idea why it will not go through when an authenticated SMTP user sends it. I can go into my Office 365 account and send email to the .co address without issue. I just cannot push it from the server. My host server does not contain the smtp server.

Sending an email in VB.Net / C# Form

In VB.Net (Or C#) when I create the code to send out an email, I always set the "sender" to an email on my server instead of the email of the person who's filling the form.
Little example:
System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
mailMessage.From = New System.Net.Mail.MailAddress("someone#domain.com")
mailMessage.To.Add(New System.Net.Mail.MailAddress("someone#domain.com"))
This is important because most hosting providers restrict using an email which relies outside of your hosting environment to send out an email (security purposes which I totally understand).
The above works really well, but the problem is when someone wants to reply to the email, they are actually going to be replying to their own email address, unless they manually copy the email of the person from the content of the message and put it in the "To" field while replying.
I would like to know what are the best practices today in order to deal sending emails.
I see some emails I get with "On Behalf Of" and others with "Reply To".
Is there anything else I am missing here? Please advice.
Working with SMTP MailMessage, i always use ReplyTo and it works:
rawMessage.ReplyTo = New MailAddress("someoneElse#domain.com");
But for outlook Interop, it's the OnBehalfOf...
EDIT:
As you are using vs2010, ReplyTo is obsolete, ReplyToList may be used:
rawMessage.ReplyToList.Add("someoneElse#domain.com");
EDIT:
Sorry I misunderstood the original question - actually it's looking for the difference between the two: OnBehalfOf and ReplyTo,
as far as I know, at the MailMessage object, if you set Sender with a different email address other than the From address, the client (outlook for instance) will show "On Behalf Of".
but if you set the ReplyTo, the client would not show that, instead, it populates the To field automatically if user clicks Reply/ReplyAll
it really depends on your needs to decide which one to use.

C# SMTP mail sending usually fails due to lack of credentials?

It seems that when you use SmtpClient class and MailMessage to send emails, everything works fine. And it does send it. However, some servers, such as your business Exchange Server or Gmail.com or whatever services, sometimes reject these emails (because they could be phishing sites or spam sites?)
I'm using this and a lot of places reject the email I believe:
SmtpClient smtp = new SmtpClient
{
Host = smtpClient,
Timeout = 40000, // 40 seconds
DeliveryMethod = SmtpDeliveryMethod.Network
};
How do you work around this? Do you have to use login credentials to some recognized / white-listed server so that it is authorized and trusted email?
I could use something like GMAIL authentication, but my server should be allowed to send emails, it shouldn't have to rely on gmail.
Long ago there were many servers on the Internet that provided the service delivery of e-mail without asking too many questions about who was using the service.
Then came the SPAMMERS!.
A server that was sending mail freely around the world was no longer a benefactor of the internet, but a problem.
Nowadays it is increasingly difficult to find a server that sends mail on the Internet without asking for credentials.
Your e-mail server (smtp.yourbusiness.com or whatever you want) does not trust you.
When you ask it to send an email for you, it wants to know who you are.
SmtpClient smtp = new SmtpClient
smtp.Host = "smtp.yourbusiness.com";
NetworkCredential credentials = new NetworkCredential("your_user_name_on_smtpserver", "your_password_on_smtpserver");
smtp.Credentials = credentials;
are you setting the sender mail address and can your mail server be resolved by reverse dns lookup? Some spam filters distrust mail servers they can't reverse lookup. If you for instance make your mail server send mails using a bogus or foreign mail domain, spam filters will probably pick this up and filter your mail. If you have set up a SPF record for your domain and your sending mail server isn't in that record, mails from that server will also often be filtered by spam filters. Another reason could be an IP address from a range known to be dynamically assigned by internet providers. Mail servers sending from these address ranges are mostly spam bots and are therefore often also filtered.
To send GMail you need to specify a number of settings - yes you'll need to use authentication (GMail wouldn't open up their relay to everyone, or they'd get used for spamming).
If I recall correctly you need to send on TCP port 587, enable SSL, host to mail.google.com and provide a username and password on your SmtpClient to get Gmail to actually send it - neglecting one of those usually generates an email back from them telling you what you've forgotten.
Edit: Just to clarify on the username and password bit; you'd need to create a new NetworkCredential with the username and password for your GMail account, and set the Credentials property of your SmtpClient to that.
If i interpret your question correctly: the messages are sent our from your server, but they are trapped in the spamfilters at the receiving end?
If this is the problem, it has nothing to to with the way you are sending the messages but much more with the content of your message and the characteristics of your server. So it does not matter what credentials you use or if you send it with SmtpClient or any other class. Moving to Gmail does not help and Gmail has a limit to the number of messages you can send.
Spam filters like spam assassin use rules. Match too many: your message is considered spam.
Have a look here for an example:
http://spamassassin.apache.org/tests_3_3_x.html
So make sure your message does not get too high a score and it will go trough.

Categories

Resources