简体   繁体   中英

C# SMTP Access Problem

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("aaaaaaaa@aaaaaaaaaaaaa.com");
                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 http://img651.imageshack.us/img651/6343/errorh.jpg

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM