Jakub Loksa
Jakub Loksa

Reputation: 577

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 [email protected], 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)

Upvotes: 0

Views: 1882

Answers (2)

TomTom
TomTom

Reputation: 62157

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.

Upvotes: 2

Steve Land
Steve Land

Reputation: 4862

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/

Upvotes: 1

Related Questions