[英]Sending mail with OUTLOOK VBScript to C#
I made a script in VBScript to send mails via Outlook that works pretty well.我在 VBScript 中制作了一个脚本来通过 Outlook 发送邮件,效果很好。 The mail's body & subject has 3 different models depending on variables entered in command windows.邮件的正文和主题有 3 种不同的模型,具体取决于在命令窗口中输入的变量。
I have a .bat
file and a .vbs
file.我有一个.bat
文件和一个.vbs
文件。 .bat
file is made for user front view and variables registry. .bat
文件是为用户前视图和变量注册表制作的。 .vbs
file is made to send the mail with the variables sent from .bat
. .vbs
文件用于发送带有从.bat
发送的变量的邮件。
My project is to enhance front view with a Windows Form Application built in C#.我的项目是使用 C# 内置的 Windows 窗体应用程序增强前视图。 A simple window with 3 radio buttons, a textbox and a button that will send the mail on_click
, with 2 variables.一个带有 3 个单选按钮、一个文本框和一个按钮的简单窗口,该按钮将发送邮件on_click
,带有 2 个变量。 var1
= would be one of the 3 models. var1
= 将是 3 个模型之一。 var2
= a "real life file" number. var2
= 一个“真实文件”编号。
Here is the VBScript code :这是VBScript代码:
Const ForReading = 1
Set args = WScript.Arguments
Dim ToAddress
Dim FromAddress
Dim CcAddress
Dim BccAddress
Dim MessageSubject
Dim MyTime
Dim MessageBody
Dim ol, ns, newMail
MyTime = Now
ToAddress = "ToAddress@somewhere.com"
FromAddress = "FromAddress@somewhere.com"
CcAddress = "CcAddress@somewhere.com"
BccAddress = "BccAddress@somewhere.com"
MessageSubject = args(0)
MessageBody = args(1)
Set ol = WScript.CreateObject("Outlook.Application")
Set ns = ol.getNamespace("MAPI")
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = MessageSubject
newMail.Body = MessageBody
newMail.Recipients.Add(ToAddress)
newMail.SentOnBehalfOfName = FromAddress
newMail.CC = CcAddress
newMail.BCC = BccAddress
newMail.Send
The difference with C# is that one variable is "set" with whichever RadioButton is checked and then, with a if(){}
function I choose which model is sent.与 C# 的不同之处在于,无论选中哪个 RadioButton,都会“设置”一个变量,然后使用if(){}
函数选择发送哪个模型。 The other variables are set with {metroTextBox1.Text}
.其他变量使用{metroTextBox1.Text}
设置。
Here's my C# code on SendButton
Click Event :这是我在SendButton
Click Event 上的 C# 代码:
private void flatCustButton012_Click(object sender, EventArgs e)
{
if (metroRadioButton1.Checked)
{
//HERE WILL GO THE C# CODE to SEND MAIL
radButtonChecked = true;
MetroFramework.MetroMessageBox.Show(this, $"Your mail has been sent successfully\nMODEL#1\nFile Nb : {metroTextBox1.Text}", "Mail sent !", MessageBoxButtons.OK);
}
if (metroRadioButton2.Checked)
{
//HERE WILL GO THE C# CODE to SEND MAIL
radButtonChecked = true;
MetroFramework.MetroMessageBox.Show(this, $"Your mail has been sent successfully\nMODEL#2\nFile Nb : {metroTextBox1.Text}", "Mail sent !", MessageBoxButtons.OK);
}
if (metroRadioButton3.Checked)
{
//HERE WILL GO THE C# CODE to SEND MAIL
radButtonChecked = true;
MetroFramework.MetroMessageBox.Show(this, $"Your mail has been sent successfully\nMODEL#3\nFile Nb : {metroTextBox1.Text}", "Mail sent !", MessageBoxButtons.OK);
}
metroTextBox1.Text = String.Empty;
}
I'm not a pro coder but I know some basics and I tried to start to convert.我不是专业编码员,但我知道一些基础知识,因此我尝试开始转换。 I'm now at the point where:我现在处于以下情况:
i need to call Outlook Application ,我需要调用Outlook 应用程序,
set the ol, ns, newMail
variables设置ol, ns, newMail
变量
and send the email并发送电子邮件
Here is where I'm at for now :这是我现在所处的位置:
For case MODEL#1 :对于案例 MODEL#1 :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MetroFramework.Forms;
using MetroFramework.Design;
using MetroFramework.Fonts;
namespace MyApplication
{
public partial class Form1 : MetroFramework.Forms.MetroForm
{
radButtonChecked = flase;
//here i declare all my variables ?
string ToAdress = "ToAdress@something.com"
string FromAddress = "FromAddress@something.com"
string CcAddress = "CcAddress@something.com"
string BccAddress = "BccAddress@something.com"
//then all my other classes / events
//public Form1() etc...
//until i come to my SendButton_Click(event)
private void flatCustButton012_Click(object sender, EventArgs e)
{
if (metroRadioButton1.Checked)
{
radButtonChecked = true;
//HERE IS THE converted CODE location
string MessageSubject = $"MODEL#1 Nb : {metroTextBox1.Text}";
string MessageBody = $"Please do MODEL#1 Nb : {metroTextBox1.Text}";
//
//REST of the convertion that i have no idea how to :
//Call OUTLOOK APPLICATION (=ol)
//getNameSpace MAPI ? (=ns)
//create mail item (= newMail)
//then all the new.Mail.Something = variables previously set with "string"
//and finally the newMail.Send
//
//REST OF THE IF :
MetroFramework.MetroMessageBox.Show(this, $"Your mail has been sent successfully\nMODEL#1\nFile Nb : {metroTextBox1.Text}", "Mail sent !", MessageBoxButtons.OK);
}
Ok so here's the result working in case anyone else is wondering how to do :好的,这是结果,以防其他人想知道该怎么做:
My "mistake" was not casting the application properly.我的“错误”没有正确地投射应用程序。
In public partial class :在公共部分类中:
string ToAdress = "ToAdress@something.com";
string FromAddress = "FromAddress@something.com";
string CcAddress = "CcAddress@something.com";
string BccAddress = "BccAddress@something.com";
Outlook.Application application = new Outlook.Application();
Then in your "event trigger"然后在你的“事件触发器”中
Outlook.MailItem newMail = (Outlook.MailItem)application.CreateItem(Outlook.OlItemType.olMailItem);
Right adter this line you just specify newMail.Something, where something = To/From/CC/BCC/Subject/Body etc..在这一行右侧,您只需指定 newMail.Something,其中 something = To/From/CC/BCC/Subject/Body 等。
And end with newMail.Send();
并以newMail.Send();
结束newMail.Send();
@BugFinder you were right, i just needed more searching and most of all testings. @BugFinder 你是对的,我只需要更多的搜索和最重要的测试。 :p :p
My deep apologies if that's not StackOverflow policy to give answers/responses like this;如果这不是 StackOverflow 政策提供这样的答案/响应,我深表歉意; please, of course, feel free to edit and/or close.当然,请随时编辑和/或关闭。
My goal is to help others.我的目标是帮助别人。 ;) ;)
Thanks.谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.