Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a small program that generates a PDF and e-mails it to the intended user, using the "localhost" as the smtp server. This works on my local machine. However when I move the program to a server the e-mail are never recieved. If I alter the program and use the ip of the mail host it will work. The problem is that the e-mail will be moving to an outside source. So there will no longer be and internal e-mail server. Here is the code simple just in case anyone has any questions. XXX are where the IP address would be.

Anyone have any ideas

private void SendMailWithAttachment(string strFrom, string strTo, string strSubject, string strBody, Attachment attOrder, Attachment attPacking)
        {
            try
            {
                MailMessage mail = new MailMessage(strFrom, strTo, strSubject, strBody);
                mail.Attachments.Add(attOrder);
                mail.Attachments.Add(attPacking);
                SmtpClient smtp = new SmtpClient("xxx.xxx.xxx.xxx");
                smtp.Send(mail);
                mail.Dispose();
            }
            catch(Exception ex)
            {
                string strError = ex.Message;
                //txtBigText.Visible = true;
                //txtBigText.Text = "Mail w/ Attachment Not Sent";
            }
        }
Posted
Updated 25-Jan-11 7:49am
v2

First guess: check up firewall settings. Could you do that?

Thank you.
--SA
 
Share this answer
 
Comments
Espen Harlinn 25-Jan-11 15:13pm    
5+ Sensible advice :)
Do you need to be authenticated to the host SMTP? see SmtpClient.Credentials[^]
 
Share this answer
 
Comments
MCY 25-Jan-11 17:50pm    
I agree. It is highly possible that the server will require an authentication, either by logged windows credentials or other.
Sergey Alexandrovich Kryukov 2-Feb-11 20:13pm    
Here come credentials - my 5.
--SA
You need to move the hostname/IP Address to either the app.config or web.config file depending on the type of application you are deploying. That way you can configure the application on deployment without having to rebuild the assemblies.
 
Share this answer
 
Comments
Espen Harlinn 25-Jan-11 15:14pm    
5+ Quality enhancement
Sergey Alexandrovich Kryukov 25-Jan-11 15:19pm    
Right, quite useful - a 5.
You'll need the SMTP server information to be configurable by your customer, rather than hard-coded in your application. The following is pulled from a project currently open in VS. It's asp.net, but the priciple is the same for desktop appslications.

  string MailHost = ConfigurationManager.AppSettings["MailHost"].ToString();
  SmtpClient client = new SmtpClient();
  client.Host = MailHost;
. . .
 
Share this answer
 
v2

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