tags:

views:

52

answers:

3

I`m working with C# using the libraries

using System.Net.Mail;
using System.Windows;

I want to use the code in many places, ie. place A, place B, place C ...

when I use it at place A, it works and mails are sent from my application.

but when I use it at place B, place C ... nothing is sent and I get errors, I want to know how to solve it.

this is my class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

using System.Net.Mail;
using System.Windows;

namespace Send_Mail_WPF_
{
    class SendMail
    {
        private string fromAddress;
        private string fromPassword;
        private string toAddress;
        private string msgSubject;
        private string msgBody;
        private string exchangeServer;
        private int exchangeServerPort;
        private bool error;

        private MailMessage message;
        SmtpClient client;

        public SendMail(string fromMail, string toMail, string fromPass, string subject, string body)
        {
            error = false;
            try
            {
                fromAddress = fromMail.ToString();
                toAddress = toMail.ToString();
                fromPassword = fromPass.ToString();
                msgSubject = subject.ToString();
                msgBody = body.ToString();
                exchangeServer = @"smtp.tedata.net";
                exchangeServerPort = 25;
                initializeMessage();
                setSMTPClient();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
                errorFound = true;
            }
        }

        public bool errorFound
        {
            set
            {
                error = value;
            }
            get
            {
                return error;
            }
        }

        private void initializeMessage()
        {
            message = new MailMessage(fromAddress, toAddress);
            message.Subject = msgSubject;
            message.Body = msgBody;
            message.IsBodyHtml = false;
        }

        private void setSMTPClient()
        {
            try
            {
                client = new SmtpClient(exchangeServer, exchangeServerPort);
                client.EnableSsl = false;
                client.Credentials = new NetworkCredential(fromAddress, fromPassword);
                MessageBox.Show("From" + message.From.ToString());
                message.From = new MailAddress("[email protected]");
                MessageBox.Show("From" + message.From.ToString());
                Application.Current.Shutdown();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
                errorFound = true;
            }
        }

        public void sendMessage()
        {
            try
            {
                client.Send(message);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
                errorFound = true;
            }
        }
    }
}

I think the problem is in the exchange server, but I don`t know how to over come this.

EDIT:

ERROR I get from any location rather than place A

alt text

A: 

By the different places, I take it you mean different machines. The problem is likely DNS, or some other problem external to your code. A good way to test SMTP connectivity is to telnet to smtp.tedata.net on port 25. I'm guessing that won't work, which explains why your code doesn't work either. Once you've solved the network issue, retry your code.

Isaac Cambron
@Isaac Cambron: Is there something like a global SMTP that I can use?
sikas
@sikas: there are many public providers; you just need to search around. Here's an old list: http://forum.spamcop.net/forums/index.php?showtopic=613
Isaac Cambron
A: 

Your code is OK. This is probably an authentication issue. Check with your network administrator.

Pierre 303
A: 

Difference places means different Ip address.

sometimes SMTP service is set to work only in limited ip pool.

In your case, it is likely your SMTP service would only work in Place A's IP address, but not in Place B, and Place C

D.J