Sending an email in VB.Net / C# Form - c#

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.

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/

Is there a way to send an email using c# without setting an smtp server?

I would like my application's users to be able to use all possible emails. Now if I think about it setting up if clauses to track their email provider in order to set an SmtpServer and SmtpServer.Port would be inefficient. Is there an easier way to do this?
I'm not sure what you mean by "track their email provider", but you absolutely do need to set an SMTP server to send email - you can't do it otherwise.
If you don't know what their SMTP server is ahead of time (which you won't if it's a desktop application, usually), the easiest thing to do is to provide a settings screen where the user sets their own SMTP information; port, SSL/not, username, password, etc; Then you will use those settings when creating the SMTP connection.
Configuration dialog to allow them to enter the ip & port?
Perhaps there's some authentication needed, hard to track that too I guess
You can use default mail client setup on computer to send emails. The caveat is that users will need to click 'send' button. Also they would be able to modify email content. This is good for feedback scenarios.
The code may be something like that:
Process.Start("mailto:name#company.com?subject=YOUR SUBJECT&body=YOUR EMAIL BODY");

Using Webbrowser control with Web proxy servers dynamically with C#

I've been following this site with a lot of admiration especially on how questions are professionally answered so I decided to be fully involved.
Please, I need urgent help on a project that I have been working on for a long time but it's almost stalled now just because of a critical issue.
An aspect of the program automates email sending to clients using the free email server systems. Due to the high frequency of email sending, I observed that the email server we're sending to drops larger parts of the emails sent out and literally blocks delivery of major emails to the recipients.
I have tried to reduce the rate of sending email out but to no avail. My fear now is my IP address might have been blocked or may be blocked soon if this continue. The program is not spamming but have to be developed in order to contact a large database of recipients at a goal within short time - like about 1000 or more recipients.
I am using Webbrowser control in C# to automate the process of logging in to the mail server and sending the email out.
Now, what I want is a sample code to use publicly available web proxy servers for each email sent out such that the source IP address appears dynamic and different to the target email server each time a message is sent out to it.
I mean, I want to dynamically get and use free public proxy servers with the Webbrowser control to send out the emails. In this way I believe the email servers would not be able to reject the emails base on the IP address source. I want to know how to dynamically get literally one web proxy server for each email sent, if possible each time.
This project is very critical and this feature is a determinant. I have googled endlessly without any straight forward solution to this issue. I would, therefore, appreciate any useful help, sample codes or resources that could help me to solve this nagging problem once and for all.
Thank you!
Your problem is "free email server systems": they consider you a spammer, and the idea you suggest (spoofing IPs) will, if detected, ruin your reputation.
If you explain what you are trying to accomplish, perhaps someone here can offer a better design.
Are you trying to give people with free email accounts (like Hotmail) bulk-emailing capabilities?
First of all (if I understood your answer right), you don't have to use WebBrowser control - you can use specified .NET solutions that allows you to efficiently sending mails:
MailMessage msg = new MailMessage("from", "to", "subject", "body text");
SmtpClient client = new SmtpClient("smtp server");
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("user", "password");
client.UseDefaultCredentials = false;
client.Credentials = cred;
Client.Send(msg);
Unfortunately, if you want to send e-mails to many recipients and you want to be sure, that these messages reach the recipients - you have to do it using your own e-mail server or do it by purchase the service on a paid e-mail servers - then they will not treat you as a spammer.
But if you anyway want to send e-mails by rotation proxy servers or similar sollution - you can define your proxy:
SmtpClient client = new SmtpClient("my.proxy_server.com", 8080);
First you have to collect any list of available proxy servers which allows you to do it in reasonable time (servers switching can significantly increase total process time because conection time can be different for each proxy server)
Proxy servers list ordered by access time:
http://www.publicproxyservers.com/proxy/list_avr_time1.html

change sender address when sending mail through gmail in 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

How to know whether I send the email successfully using System.Net.Mail

I use C# to send email using System.Net.Mail.
But How can I know whether the email has been sent successfully or fail?
Not a direct answer of course, but here is a great article from Coding Horror about sending emails through code, which addresses your question.
The simple answer is that if it's a detectable failure then SmtpClient.Send will throw an SmtpFailedRecipientsException (it will throw an SmtpException if there is a connection problem).
The long answer is that, for many reasons, you won't necessarily get an exception. This can be because your SMTP server is buffering the sends (or if you have it set to local iis or local directory), or simply because the SMTP server does not support returning those error codes.
Most advertising mailing systems track bouncebacks by setting a valid "from address" and monitoring the inbox for that account. Even that, however, is not foolproof (out of office messages, for example).
I would stick to catching exceptions and leaving the others. If you're not sure if emails are valid, maybe you should send users a code to have them validate their email address.
Considering Emails are fire-and-forget, this can be quite hard. Unless you get an exception, denoting a client-server communication issue due to wrong credentials etc, you have no means of knowing if a mail was sucessfully send (meaning: arrived at the addressee).
I've personally never come across an answer to this as there's not really a way you can grantee mail leaving your location is'nt going to get thrown away as soon as it leaves you. You could maybe try and check the sending mailbox for an NDR (none delivery report) but your not always granted to get one
If you mean sent successfully then as other posters say, if it cannot be sent then you will get an exception. If you actually mean recieved succesfully then that is an entirely different matter. SMTP simply does not have a mechanism to find out if the message has been recieved by the recipient, sometimes you will get an ndr if a server can't deliver other times you won't. You could try and do what spammers do and that is to put some kind of image link in your html email body with a unique identifier in the query string. That would allow you to link someone hitting the image url with the email. However that would only tell you the email had been read if the url was hit, it would not tell you that the email hadn't been recieved becuase the recipient might simply not have allowed images to be displayed in their email client.

Categories

Resources