qt & c# & wpf 调用outlook客户端发送邮件

3 篇文章 0 订阅
3 篇文章 0 订阅

折腾三天,找到两种调用客户端发送邮件功能。
我的需求是类似android发邮件方式,把需要发送的内容发给邮箱客户端,客户自己点击发送。这样程序就不需要考虑登录、各种协议授权等问题,也不同考虑发送附件问题。
1、第一种方法调用OUTLOOK发送邮件,这种必须安装微软office套件
Microsoft.Office.Interop.Outlook;需要添加引用。我的电脑VS2019添加引用也没有[Microsoft Office 15.0 Object Library],最后我在NuGet中搜索一个添加的。
使用c#制作exe,然后 qt 直接调用即可

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
using Outlook = Microsoft.Office.Interop.Outlook;



namespace SendingEmailFromClients
{
    class Program
    {
        static void Main(string[] args)
        {

            //send email from outlook
            sendEmailByOutlook(args);
        }

        //method to send email to outlook
        public static void sendEmailByOutlook(string[] args)
        {
            //SendingEmailFromClients.exe -s "Your subject will go here" -a "D:\1.jpeg" -r "zhangpeng@foxwelltech.com" -b "Hello rollingman your message body will go here!"
            int i = 0;
            string Subject = string.Empty;
            string Attachment = string.Empty;
            string Body = string.Empty;
            string Recipients = string.Empty;

            foreach (string argument in args)
            {
                if (args[i].ToLower() == "-s" || args[i].ToLower() == "--subject")
                {
                    Subject = args[i + 1];
                    Console.WriteLine("Subject is " + Subject);
                }

                if (args[i].ToLower() == "-a" || args[i].ToLower() == "--attachment")
                {
                    Attachment = args[i + 1];
                    Console.WriteLine("Attachment is " + Attachment);
                }

                if (args[i].ToLower() == "-r" || args[i].ToLower() == "--recipients")
                {
                    Recipients = args[i + 1];
                    Console.WriteLine("Recipients is " + Recipients);
                }

                if (args[i].ToLower() == "-b" || args[i].ToLower() == "--body")
                {
                    Body = args[i + 1];
                    Console.WriteLine("Body is " + Body);
                }
                i++;
            };

            try
            {
                // Create the Outlook application.
                Outlook.Application oApp = new Outlook.Application();
                // Create a new mail item.
                Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
                //  Set HTMLBody. 
                //add the body of the email
                oMsg.HTMLBody = Body;
                //Add more attachment.
                string[] list = Attachment.Split(';');
                int iPosition = 0;
                foreach (string bean in list)
                {
                    String sDisplayName = bean;

                    if (!string.IsNullOrEmpty(oMsg.Body))
                    {
                        iPosition = (int)oMsg.Body.Length;
                    }
                    else
                    {
                        ++iPosition;
                    }

                    int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
                    //now attached the file
                    Outlook.Attachment oAttach = oMsg.Attachments.Add(bean, iAttachType, iPosition, sDisplayName);
                }
                //Subject line
                oMsg.Subject = Subject;
                // Add a recipient.
                Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
                // Change the recipient in the next line if necessary.
                Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(Recipients);
                oRecip.Resolve();
                //显示outlook客户端
                oMsg.Display();
                // Send.不显示客户端直接发送
                //oMsg.Send();

                // Clean up.
                oRecip = null;
                oRecips = null;
                oMsg = null;
                oApp = null;
            }//end of try block
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }//end of catch

        }//end of Email Method

2、第二种方式是使用firefox 的 Mozilla Thunderbird工具,安装完成后配置客户端
直接cmd -->找到安装路径–>thunderbird -compose “to=zhangpeng@foxwelltech.com,subject=you email here,body=this is test,attachment=D:\1.jpeg”
就可以发送邮件了,注意:必须逗号,必须等号,必须引号,不能有空格(不包括内容)
使用qt 的 QProcess::startDetached(cmd);即可,注意必须先配置好客户端

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值