Link to home
Start Free TrialLog in
Avatar of Elham_Seif
Elham_Seif

asked on

Adding "Send to a friend" function to my website like www.BBC.COM

Please read to the end to see what my proble is.
I have no problem sendig emails.
I'd like to add "Send to a friend" function to my website.
This function will get three things from the user:
1- His/her Name.
2- His/her Email address.(like myemail@dddd.com)
3- His/her friend Email address.(like myFriend@ddd.com)
Then I send an email to myFriend@ddd.com and add a text as the body of the email.
What I'd like to do is that I want to put myemail@dddd.com (user's email address) as the Sender of my email but It gives me the error. I have to set the Sender to an email from my website that I'v authenticate it in SMTP.
Some websites are doing it. For example www.BBC.com, if you try to "send to a friend", it'll set your email address as the Sender of the email to your friend just like you logged in to your email and send an email to your friend!
Do you know how they can do it. And what is the way I can do it too.
Thanks in advance for your time and answers

MailMessage message = new MailMessage();        
      //  message.From = new MailAddress("myemail@dddd.com"); // it's what I'd like to do but it give me an error, So I have to set the sender to an email from my website.
       message.From = new MailAddress("contact@sharplabelcutter.com");        
        message.To.Add(new MailAddress("myFriend@ddd.com"));            
        SmtpClient client = new SmtpClient();            
 
               try
        {
            message.Body = Mytext ;
            message.Subject = "send to a friend";
            client.Send(message);
            
                    }
        catch
        {
         giveError();
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of RiteshShah
RiteshShah
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jar3817
jar3817

You're probably getting an error because you're effectively forging the FROM address on this email, and in this day and age of spam, that is not a good thing to do.

Try using one of your email addresses as the from address but with their name associated with it:

message.From = new MailAddress("Mike Smith <contact@sharplabelcutter.com>");

Obviously you should substitute the first and last name from your web form. This way when the email lands in the recipient's box, it just says it's from "Mike Smith" who they know, but the actual from address is you (which it should be). You can also set a reply-to address to that of the person filling out the form, that why if the recipient replies with a "thanks" it goes to the original person using the website rather than you.

Avatar of Elham_Seif

ASKER

Hi  jar3817
I agree with you but how come www.BBC.com can do it? Did you test it?
It's interesting for me how they can do it. Your freind will receive an email which it's Sender is you instead of BBC!!
Elham_Seif,

what about the script I gave you? have you tried it?
Hi RiteshShah
I'd like to test your code but I don't know what to set for yournetworkAuthenticationID in line:
      System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("yournetworkAuthenticationID/Email", "Password");
How can I find it?
if you are using your own email server than use it's ID/Pass and if you are using service from vendor than tell them you want to send "Authenticated email" from your script, what should be the credentials, they will help you for sure.
BBC can do it because they don't care if the message actually gets delivered. I just tried on their site and the message landed in my spam box because their system breaks things like SPF validation. Their server isn't authorized to send mail using my domain in the FROM address.
Hi RiteshShah
Thanks for your comments. I should ask my vendor about credentials. On Satarday I can ask them and check to see if it works. I really hope so.
Hi JAR3817
Thanks for your checking BBC. I have tested it in many cases. Yahoo emails and my own website get the emails correctly in my inbox. But sometimes they go to SPAM folder.
It seems that they shouldn't be authorized to do this but that's what they're doing!
And if they can do this trick, we should also be able to do.
In you original post, you said it gives you an error when trying to spoof the sender address. What exactly is the error?
Thanks.
The first code worked for me.
System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage(mailFrom, mailTo);
     email.Subject = mySubject;
     email.IsBodyHtml = true;
     email.Body = myEmailBody;

     System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient();
     System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("anEmailFromMyHost", "myPassword", "myHost");    
     mailClient.Port = 25;
     mailClient.UseDefaultCredentials = false;
     mailClient.Credentials = basicAuthenticationInfo;

     try
     {
         mailClient.Send(email);
     }
     catch (Exception ex)
     {
         return ex.ToString();
     }