如何知道发送SMTP电子邮件的主机地址 - Windows服务 [英] How to Know The Host Adress to Send SMTP Email - Windows Service

查看:236
本文介绍了如何知道发送SMTP电子邮件的主机地址 - Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Windows服务,每天自动发送电子邮件,安装服务,一切都很好的服务,它开始,写入日志文件和所有,但主要的事情,发送电子邮件功能不管用。这是我用来发送电子邮件的
代码。


我的猜测是我在Smtp.Host地址中有一个错误的地址,那么在哪里可以找到它,我做了一个谷歌搜索我被要求去Outlook工具 - 电子邮件帐户 - Chnage,我看到有我的Microsoft Echange Server地址和用户名,我没有看到
其他任何东西。


请帮帮我..这是我用于Windows服务的代码。


如果有人可以帮我处理这段代码或者给我这将是非常有帮助的。我使用Windows服务发送电子邮件的任何工作代码..他们的cuople cuople在codeproject上可用,但他们没有使用System


使用System;

使用系统.Collections.Generic;

使用System.ComponentModel;

使用System.Data;

使用System.Diagnostics;

使用System.ServiceProcess;

使用System.Text;

使用System.Net.Mail;



命名空间SimpleWindowsService

{

 &nbs磷;   InitializeComponent();

            if(!System.Diagnostics.EventLog.SourceExists(" SimpleSource")))
              ;   System.Diagnostics.EventLog.CreateEventSource(" SimpleSource"," SimpleLog");

            // SimpleSource将在eventviewer中显示为coloumn。

            eventLogSimple.Source =" SimpleSource";

            // SimpleLog将显示为事件文件夹。

            eventLogSimple.Log =" SimpleLog"; $
    public partial class SimpleService:ServiceBase

    {

        DateTime mdt = DateTime.Now;

        public SimpleService()

        {


}



        protected override void OnStart(string [] args)

        {

            //  代码在这里开始你的服务。



            eventLogSimple.WriteEntry(QUOT;服务开始"!);


SmtpClient SClient =新SmtpClient();

             System.Net.Mail.MailMessage msgMail =新System.Net.Mail.MailMessage();

        

            SClient.Host =" " ;;

            SClient.Port = 25;

            MailAddress strFrom =新MailAddress(QUOT; xyz@gmail.com");



             string strTo =" xyz@mydomain.com" ;;

            string strSubject =" Test"; $
            string strEmailBody =" Test"; $


            msgMail.Body = strEmailBody;

            msgMail.Subject = strSubject;

            msgMail.To.Add(strTo);

            msgMail.From = strFrom;

            msgMail.IsBodyHtml = true;

            if(strTo.Trim()!="")

            {

               试试
                {

                    SClient.Send(msgMail);

                 &NBSP ;  eventLogSimple.WriteEntry(QUOT;发送");



                 }
                catch

                {

                    eventLogSimple.WriteEntry(QUOT;失败");



                 }
            }

解决方案

如果您使用gmail ID发送电子邮件那么以下链接中的代码应该可以帮到你。 Gmail的端口为587,需要SSL。


http://gauravkhanna.blog.co.in/2008/07/13/send-email-from-your-gmail-account/


I am Writing a Windows Service To Send Email Automatically Every Day, Installed the Service and Everything is fine with the service, it is starting, Writing to a Log file and All , But the Main Thing, Sending Email Functionality is not working. Here is teh code i am using to send email.

My Guess is that I have a Wrong address in The Smtp.Host Address, So Where can i find it, I did a Google Search And i was asked to go to Outlook Tools-Email Accounts-Chnage , all i see there is My Microsoft Echange Server addres and User Name, I dont see anything else.

Please help me Out.. This is the code I am using for the Windows Service.

It will be of great help if some one can help me with this code or Give me any working code to send out email using windows service.. Itried cuople of them Available on codeproject, but they didnt work out

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Net.Mail;

namespace SimpleWindowsService
{
    InitializeComponent();
            if (!System.Diagnostics.EventLog.SourceExists("SimpleSource"))
                System.Diagnostics.EventLog.CreateEventSource("SimpleSource", "SimpleLog");
            // SimpleSource will appear as a coloumn in eventviewer.
            eventLogSimple.Source = "SimpleSource";
            // SimpleLog will appear as an event folder.
            eventLogSimple.Log = "SimpleLog";
    public partial class SimpleService : ServiceBase
    {
        DateTime mdt = DateTime.Now;
        public SimpleService()
        {

}

        protected override void OnStart(string[] args)
        {
            //   code here to start your service.

            eventLogSimple.WriteEntry("service started!");

SmtpClient SClient = new SmtpClient();
            System.Net.Mail.MailMessage msgMail = new System.Net.Mail.MailMessage();
        
            SClient.Host = " ";
            SClient.Port = 25;
            MailAddress strFrom = new MailAddress("xyz@gmail.com");

            string strTo = "xyz@mydomain.com";
            string strSubject = "Test";
            string strEmailBody = "Test";

            msgMail.Body = strEmailBody;
            msgMail.Subject = strSubject;
            msgMail.To.Add(strTo);
            msgMail.From = strFrom;
            msgMail.IsBodyHtml = true;
            if (strTo.Trim() != "")
            {
                try
                {
                    SClient.Send(msgMail);
                    eventLogSimple.WriteEntry("Sent");

                }
                catch
                {
                    eventLogSimple.WriteEntry("Failed");

                }
            }

解决方案

If you are sending email using your gmail id then code in following link should help you. Gmail has port 587 and require SSL.

http://gauravkhanna.blog.co.in/2008/07/13/send-email-from-your-gmail-account/


这篇关于如何知道发送SMTP电子邮件的主机地址 - Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆