• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1398
  • Last Modified:

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

0
Elham_Seif
Asked:
Elham_Seif
  • 5
  • 3
  • 3
1 Solution
 
RiteshShahCommented:
can't you use below function? for more details, have a look at my small article at:

http://www.sqlhub.com/2008/05/email-sending-script-from-c.html



public string AuthenticatedMailSend(string mailFrom,string mailTo, string Subject, string MessageBody)
        {
            mailFrom = "myemail@dddd.com";
            mailTo = "myFriend@ddd.com";
 
            System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage(mailFrom, mailTo);
            email.Subject = Subject;
            email.IsBodyHtml = true;
            email.Body = MessageBody;
 
            System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient();
            System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("yournetworkAuthenticationID/Email", "Password");
            mailClient.Host = "";
            mailClient.Port = 25;
            mailClient.UseDefaultCredentials = false;
            mailClient.Credentials = basicAuthenticationInfo;
 
            try
            {
                mailClient.Send(email);
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
 
            return "1";
        }

Open in new window

0
 
jar3817Commented:
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.

0
 
Elham_SeifAuthor Commented:
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!!
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
RiteshShahCommented:
Elham_Seif,

what about the script I gave you? have you tried it?
0
 
Elham_SeifAuthor Commented:
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?
0
 
RiteshShahCommented:
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.
0
 
jar3817Commented:
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.
0
 
Elham_SeifAuthor Commented:
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.
0
 
Elham_SeifAuthor Commented:
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.
0
 
jar3817Commented:
In you original post, you said it gives you an error when trying to spoof the sender address. What exactly is the error?
0
 
Elham_SeifAuthor Commented:
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();
     }
 
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now