C# MailMessage Display email instead of Send - c#

I have been following the example below to send email using C# code with success:
MSDN Mail Message
However, I would like the code to display the composed email message on user machine, so that user can have a final check before hitting send button on outlook.
In the VBA world, I can use mail.Display in place of mail.Send.
Can anyone provide some advice to achieve that in C#?
Thanks.

How about this...
private void btnEmail_Click(object sender, EventArgs e)
{
string command = "mailto:somebody#domain.com?subject=The Subject&bcc=another#codegaim.com&body=Hi,I found this website and thought you might like it http://www.geocities.com/wowhtml/";
Process.Start(command);
}

Found a great solution to my problem i.e. to use Microsoft Office Interop Outlook instead of System.Net.MailMessage
followed this:
How to send a mail using Microsoft.Office.Interop.Outlook.MailItem by specifying the From Address
//using Microsoft.Office.Interop.Outlook;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Outlook;
namespace ConsoleApplication1
{
using Outlook = Microsoft.Office.Interop.Outlook;
public static class Program
{
static void Main(string[] args)
{
SendUsingAccountExample();
}
private static void SendUsingAccountExample()
{
var application = new Application();
var mail = (_MailItem)application.CreateItem(OlItemType.olMailItem);
mail.Body = "Hello";
mail.Subject = "Good Bye";
mail.To = "hello#google.com";
// Next 2 lines are optional. if not specified, the default account will be used
Outlook.Account account = Application.Session.Accounts["MyOtherAccount"];
mail.SendUsingAccount = account;
mail.Display(false); // To Display
//mail.Send(); // To Send
}
}
}

Related

Changing outlook mail sender in C# using Microsoft.Office.Interop.Outlook.Mailitem

I programmed a wpf application that sends an outlook mail through the default logged in user.
This is my code:
using System;
using System.Configuration;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Runtime.InteropServices;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
namespace FileOrganizer
{
class Program
{
private void CreateMailItem()
{
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem mailItem = app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
mailItem.Subject = "This is the subject";
mailItem.To = "someone#example.com";
mailItem.Body = "This is the message.";
mailItem.Attachment.Add(logPath);//logPath is a string holding path to the log.txt file
mailItem.Importance = Microsoft.Office.Tools.Outlook.OlImportance.olImportanceHigh;
mailItem.Display(false);
}
}
}
I saw this post:
How to send a mail using Microsoft.Office.Interop.Outlook.MailItem by specifying the From Address
But i can't find the Outlook.Accounts option in my project. It just doesn't appear.
Does anyone have a solution for this problem?
My main goal of course is to send my mail using different user.

Adding text to an email using an Outlook addin C# 2010

I want to add some text to the body of my email. But i keep getting this error:
Error 1 'EmailHelper.EmailHelperRibbon' does not contain a definition for 'Application' and no extension method 'Application' accepting a
first argument of type 'EmailHelper.EmailHelperRibbon' could be found
(are you missing a using directive or an assembly reference?)
My code looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using System.Windows.Forms;
namespace EmailHelper
{
public partial class EmailHelperRibbon
{
private void EmailHelperRibbon_Load(object sender, RibbonUIEventArgs e)
{
}
private void button1_Click(object sender, RibbonControlEventArgs e)
{
System.Windows.Forms.MessageBox.Show("Your Ribbon Works!");
Microsoft.Office.Interop.Outlook.MailItem mailItem = (Microsoft.Office.Interop.Outlook.MailItem)
this.Application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
{
mailItem.Subject = "This text was added by using code";
mailItem.Body = "This text was added by using code";
}
}
}
}
Try this one:
var inspector = this.Context as Outlook.Inspector;
var currentMailItem = inspector.CurrentItem as Outlook.MailItem;
currentMailItem.Body = "your text";
I am guessing this is a Ribbon based on the name, so you need to access the application off of the Globals class:
Globals.ThisAddIn.Application.CreateItem(...)

CSharp - IMAP Connection TCP error

I wanted to access over IMAP to my gmail account bsed on this example:
http://code.msdn.microsoft.com/windowsdesktop/Simple-IMAP-CLIENT-b249d2e6
When I start to debug the code, I get always an error at the codeline:
tcpc = new System.Net.Sockets.TcpClient("imap.gmail.com", 993);
It gives me the error:
"The requested service provider could not be loaded or initialized"
My search on Internet give me as result, that I need to repair the Window Socket.
But, when I run directkly the compiled .exe file, then I don't get this error at all.
This makes no sense to me.
Does anybody have an idea, whats wrong here?
The code looks like:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
namespace IMAP
{
class Program
{
static System.Net.Sockets.TcpClient tcpc = null;
static System.Net.Security.SslStream ssl = null;
static string username, password;
static void Main(string[] args)
{
username = "(username)";
password = "(password)";
SSLAuthenticate = "imap.gmail.com";
try
{
tcpc = new System.Net.Sockets.TcpClient("imap.gmail.com", 993);
ssl = new System.Net.Security.SslStream(tcpc.GetStream());
ssl.AuthenticateAsClient("imap.gmail.com");
...
I am thankful for any help.

does not contain a definition for 'Form' ? Outlook Add-in

Recently I' am working on an add-in with c# for Outlook 2010 in Visual Studio 2012 . I developed a custom Form region which contains simple textboxes and a button. In Button_click method I' am taking an error (error Code : error CS0117) and I couldn't figure out why ? I am really new of this environment :) any way here it is my problem. (if you need more code to solve just let me know)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Office = Microsoft.Office.Core;
using Outlook = Microsoft.Office.Interop.Outlook;
using MSForms = Microsoft.Vbe.Interop.Forms;
.
.
.
private void button1_Click(object sender, EventArgs e)
{
// save button
MSForms.UserForm userForm = (MSForms.UserForm) FormRegion1.Form;
MSForms.Controls formControls = userForm.Controls;
Outlook.OlkTextBox ad =
(Outlook.OlkTextBox)formControls.Item("ad");
string cariad = ad.Text;
Outlook.OlkTextBox adres =
(Outlook.OlkTextBox)formControls.Item("adres");
string cariadres = adres.Text;
Outlook.OlkTextBox vergid =
(Outlook.OlkTextBox)formControls.Item("vergid");
string carivergid = vergid.Text;
.
.
.
*MSForms.UserForm userForm = (MSForms.UserForm) in line FormRegion1.Form "Form" is not recognized. And error says
" error CS0117: 'OutlookAddIn2.FormRegion1' does not contain a
definition for 'Form'".
Thanks a lot.
I solved that problem with not using Forms:) Simply, I just typed
this.TextBoxName.Text;
to reach the text of the textBox. Thanks any way.

How to add login screen in my email application & login to my email account (windows phone 7)

I created a simple email application for Windows Phone 7, but I need to set-up my email account to send email from my application.
I want to add a login screen in my application so I can set-up my email account and when I put my email user id or password my application should take me to the compose mail screen.
I also want it to remember my user id or password until I logout.
This is my application code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
namespace SendingMail
{
public partial class MainPage : PhoneApplicationPage
{
EmailAddressChooserTask emailAddresstask;// Constructor
public MainPage()
{
InitializeComponent();
this.emailAddresstask = new EmailAddressChooserTask();
this.emailAddresstask.Completed += new EventHandler<EmailResult>(emailAddresstask_Completed);
}
#region Events
//Open Contact button click
private void btnOpenContact_Click(object sender, RoutedEventArgs e)
{
emailAddresstask.Show();
}
//Email Address Chooser Task Completed
private void emailAddresstask_Completed(object sender, EmailResult e)
{
if (e.TaskResult == TaskResult.OK)
{
txtTo.Text = e.Email;
}
}
//Send mail button click
private void btnMail_Click(object sender, RoutedEventArgs e)
{
EmailComposeTask Myemail_Composetask = new EmailComposeTask();
Myemail_Composetask.To = txtTo.Text;
Myemail_Composetask.Cc = txtCC.Text;
Myemail_Composetask.Subject = txtSbj.Text;
Myemail_Composetask.Body = txtbd.Text;
Myemail_Composetask.Show();
}
#endregion
}
}
I guess you have mis-understood the how does Email Launcher task works. It does not have any login, or log out options. This Email Launchers task will be used to compose a email on configured email on outlook or other POP, IMAP email application.
Also you will be not able to test on emulator for this. You can only test on the device which has configured at least one email in it.
Thanks,
Kamal.

Categories