Reputation: 148
I made a script in VBScript to send mails via Outlook that works pretty well. The mail's body & subject has 3 different models depending on variables entered in command windows.
I have a .bat
file and a .vbs
file.
.bat
file is made for user front view and variables registry.
.vbs
file is made to send the mail with the variables sent from .bat
.
My project is to enhance front view with a Windows Form Application built in C#.
A simple window with 3 radio buttons, a textbox and a button that will send the mail on_click
, with 2 variables.
var1
= would be one of the 3 models.
var2
= a "real life file" number.
Here is the VBScript code :
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 = "[email protected]"
FromAddress = "[email protected]"
CcAddress = "[email protected]"
BccAddress = "[email protected]"
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.
The other variables are set with {metroTextBox1.Text}
.
Here's my C# code on SendButton
Click Event :
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,
set the ol, ns, newMail
variables
and send the email
Here is where I'm at for now :
For case 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 = "[email protected]"
string FromAddress = "[email protected]"
string CcAddress = "[email protected]"
string BccAddress = "[email protected]"
//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);
}
Upvotes: 0
Views: 424
Reputation: 148
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 = "[email protected]";
string FromAddress = "[email protected]";
string CcAddress = "[email protected]";
string BccAddress = "[email protected]";
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..
And end with newMail.Send();
@BugFinder you were right, i just needed more searching and most of all testings. :p
My deep apologies if that's not StackOverflow policy to give answers/responses like this; please, of course, feel free to edit and/or close.
My goal is to help others. ;)
Thanks.
Upvotes: 1