简体   繁体   English

C#SMTP访问问题

[英]C# SMTP Access Problem

I`m working with C# using the libraries 我正在使用库使用C#

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

I want to use the code in many places, ie. 我想在很多地方使用代码,即。 place A, place B, place C ... 地方A,地方B,地方C ...

when I use it at place A, it works and mails are sent from my application. 当我在A处使用它时,它可以工作,并且邮件是从我的应用程序发送的。

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. 但是当我在B处,C处使用它时...什么也没发送,并且出现错误,我想知道如何解决它。

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 错误我从任何地方而不是地方A到达

alt text http://img651.imageshack.us/img651/6343/errorh.jpg 替代文字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. 问题可能出在DNS或代码外部的其他问题。 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. 测试SMTP连接的一种好方法是通过telnet到端口25上的smtp.tedata.net。我猜想它不起作用,这说明了为什么您的代码也不起作用。 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. 不同的地方意味着不同的IP地址。

sometimes SMTP service is set to work only in limited ip pool. 有时SMTP服务设置为仅在有限的IP池中工作。

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 在您的情况下,您的SMTP服务可能仅在场所A的IP地址中起作用,而在场所B和场所C中不起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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