Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
As title. I am developing a system that is able to send email with attachments to members. I want the receivers can't reply the received email because the sender email address may be virtual , like some email that contains "this email is sent by the system automatically, please do not reply."
I want to build this function but I found it is hard without 3rd party components, and those are expensive. I've tried some sample codes, one of this is like the below:
C#
public string SendEMail(string From,string To,string Subject,string Content)
        {
            try
            {
                System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
                msg.To.Add(To);
                msg.From = new MailAddress(From, From, System.Text.Encoding.UTF8);

                msg.Subject = Subject; 
                msg.SubjectEncoding = System.Text.Encoding.UTF8; 
                msg.Body = Content; 
                msg.BodyEncoding = System.Text.Encoding.UTF8; 
                msg.IsBodyHtml = false; 
                msg.Priority = MailPriority.Normal;

                SmtpClient client = new SmtpClient();
                //client.Credentials = new System.Net.NetworkCredential(SenderAddress, SenderPassword);
                client.Host = "localhost";
                //client.EnableSsl = true;

                client.Send(msg);
                return string.Empty;
            }
            catch (Exception ex)
            {
                return "Exception:" + ex.Message + "Stacktrace:" + ex.StackTrace;
            }
        }

But I got the "Send EMail fail." exception message while executing this code. How could I get it?
Posted
Updated 30-Nov-15 18:59pm
v3
Comments
PIEBALDconsult 26-Nov-15 20:40pm    
That's simple; .net has all you need. I refer you to the System.Net.Mail namespace in general and the MailMessage class specifically.
Momoko Asahina 708H 26-Nov-15 20:44pm    
I knew, but I tried to use many sample codes and fails. May I have more details about this way?
PIEBALDconsult 26-Nov-15 20:47pm    
It's been a while since I last did it. Perhaps you should use Improve question to add the code you tried and explain how it didn't work, etc.
PIEBALDconsult 26-Nov-15 21:07pm    
Ah, OK, better. Is your localhost an SMTP server? If not, don't use it as the client.Host .
Momoko Asahina 708H 26-Nov-15 21:21pm    
No, my system will run at normal desktop PC with Windows Desktop System.

1 solution

You have a few problems. First, you have no control over the mail server you're using unless you set one up yourself. This means you are at the mercy of the policies setup on the mail server you're using to send the emails. No, you can't do anything without one.

This means that if the owner of the mail server says that it will not send or forward an email without a sender address, you have to put one in. If the owner says that the email address MUST match the account that is sending the email, you MUST put your email address in the sender field.

Where I work, it's impossible to send and email without the sender address filled in. It doesn't have to be a valid account. It just has to be filled in.

Now that you've gotten your email onto the server that is sending your mail, you have no defeated those problems. Some servers will not even deliver an email if the sender is not filled in. So leaving the field blank may not work for all of your recipients.

The best method to do what you want is to just create an email address (it usually doesn't even have to exist!) that says "noreply@yourdomain.com" or something similar. If the user replies to the email, that's their problem, not yours. Their email will not be deliverable if the email account doesn't exist. At worst, it'll sit in the inbox of an account that nobody monitors and eventually be deleted by the server.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900