通过WebService的发送邮件 [英] Send Email by WebService

查看:395
本文介绍了通过WebService的发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Windows应用程序开发。现在我需要通过Web服务发送电子邮件(包括附件功能)。我该怎么做?

此外,我需要之前,'N'天通知邮件。 ('N'天是由用户控制的功能)

让我知道如果任何评论。

感谢。


解决方案

 公共BOOL发送(字符串的toAddress,弦乐主题,绳体,布尔isHtml,列表与LT;串>文件)
{
    尝试
    {
        MAILMSG MAILMESSAGE新= MAILMESSAGE();        mailMsg.To =的toAddress;
        mailMsg.Headers.Add(从的String.Format({0}< {1}>中,发出者,senderAddress));
        mailMsg.Fields [http://schemas.microsoft.com/cdo/configuration/smtpserver] =服务器;
        mailMsg.Fields [http://schemas.microsoft.com/cdo/configuration/smtpserverport] =口;
        mailMsg.Fields [http://schemas.microsoft.com/cdo/configuration/sendusing] = 2;        如果(enableAuth)
        {
            mailMsg.Fields [http://schemas.microsoft.com/cdo/configuration/smtpauthenticate] = 1;
            mailMsg.Fields [http://schemas.microsoft.com/cdo/configuration/sendusername] =用户名;
            mailMsg.Fields [http://schemas.microsoft.com/cdo/configuration/sendpassword]​​ =密码;
        }        如果(enableSsl)
        {
            mailMsg.Fields.Add(http://schemas.microsoft.com/cdo/configuration/smtpusessl,真);
        }        如果(isHtml)
        {
            mailMsg.BodyFormat = MailFormat.Html;
        }        mailMsg.BodyEncoding = Encoding.UTF8;
        mailMsg.Subject =主体;
        mailMsg.Body =身体;        的for(int i = 0; I< files.Count;我++)
        {
            mailMsg.Attachments.Add(新MailAttachment(文件[I]));
        }
        SmtpMail的SmtpServer =服务器;
        SmtpMail.Send(MAILMSG);        返回true;
    }
    赶上(异常前)
    {
        this.errorMsg = ex.Message;
        返回false;
    }
}

请注意,您必须使用System.Web.Mail这个鳕鱼的工作。

I have developed on Windows Application. Now i need to send an email (attachment feature included) by Web Service. How can i do that?

Also i need to notify the email before 'n' days. ('n' days is a feature controlled by user)

Let me know if any comment.

Thanks.

解决方案

public bool Send(string toAddress, string subject, string body, bool isHtml, List<string> files)
{
    try
    {
        MailMessage mailMsg = new MailMessage();

        mailMsg.To = toAddress;
        mailMsg.Headers.Add("From", string.Format("{0} <{1}>", senderName, senderAddress));
        mailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = server;
        mailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = port;
        mailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;

        if (enableAuth)
        {
            mailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
            mailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = userName;
            mailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = password;
        }

        if (enableSsl)
        {
            mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
        }

        if (isHtml)
        {
            mailMsg.BodyFormat = MailFormat.Html;
        }

        mailMsg.BodyEncoding = Encoding.UTF8;
        mailMsg.Subject = subject;
        mailMsg.Body = body;

        for (int i = 0; i < files.Count; i++)
        {
            mailMsg.Attachments.Add(new MailAttachment(files[i]));
        }
        SmtpMail.SmtpServer = server;
        SmtpMail.Send(mailMsg);

        return true;
    }
    catch (Exception ex)
    {
        this.errorMsg = ex.Message;
        return false;
    }
}

Note that you must use System.Web.Mail for this cod to work.

这篇关于通过WebService的发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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