C#Outlook邮件推送

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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);即可,注意必须先配置好客户端

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值