In my Windows Phone 7 application I want to send an e-mail where the message body should contain the data from my previous page in my application. Previously I just integrated the e-mail facility like this:
private void Image_Email(object sender, RoutedEventArgs e)
{
EmailComposeTask emailComposeTask = new EmailComposeTask();
emailComposeTask.Subject = "message subject";
emailComposeTask.Body = "message body";
emailComposeTask.To = "recipient#example.com";
emailComposeTask.Cc = "cc#example.com";
emailComposeTask.Bcc = "bcc#example.com";
emailComposeTask.Show();
}
But I was not able to test this in my emulator. Now in the body part I want my data from the previous page. So how to do this?
Updated code:
if (this.NavigationContext.QueryString.ContainsKey("Date_Start"))
{
//if it is available, get parameter value
date = NavigationContext.QueryString["Date_Start"];
datee.Text = date;
}
if (this.NavigationContext.QueryString.ContainsKey("News_Title"))
{
//if it is available, get parameter value
ntitle = NavigationContext.QueryString["News_Title"];
title.Text = ntitle;
}
if (this.NavigationContext.QueryString.ContainsKey("News_Description"))
{
ndes = NavigationContext.QueryString["News_Description"];
description.Text = ndes;
}
Now what do I write in the message body? I am not able to test it as I do not have a device.
Can i pass in the values like this:
emailComposeTask.Body = "title, ndes, date";
I think the code is correct. if you want to pass body from previous page, you need to pass it when page navigation. and set emailComposeTask.Body = yourPassedValue.
like this:
var date;
var title;
var ndes;
emailComposeTask.Body = title + "," + ndes + "," + date;
You need to edit your message body line like this:
emailComposeTask.Body = title+" "+ ndes+" "+ date;
You cannot test sending mail in the emulator since you don't have a proper email account set up. Nor you could set it up in the emulator.
The Body property is a string so you can put inside pretty much anything you want.
Using the following code will only generate a string containing exactly that:
emailComposeTask.Body = "title, ndes, date";
So the result mail will have a body containing "title, ndes, date" as a text. If you want to replace the title with the value from the local variable named title, you need to use the following syntax:
emailComposeTask.Body = string.Format("{0}, {1}, {2}", title, nodes, date);
Related
I have managed to create simple app that will send e-mail with specific text, but I am wondering that is this possible to send the same e-mail, but with content of text being copied into clipboard?
In my oMail.TextBody I would like to paste content of clipboard and e-mail it.
static void Main(string[] server)
{
SmtpMail oMail = new SmtpMail("TryIt");
EASendMail.SmtpClient oSmtp = new EASendMail.SmtpClient();
// Set sender email address
oMail.From = "myEmail";
// Set recipient email address
oMail.To = "myEmail";
// Set email subject
oMail.Subject = "test email from c# project";
// Set email body
oMail.TextBody = "Clipboard content pasted here..."
}
Is there any way to do it? Additionally I am using EASendMail namespace.
In console app, clipboard is accessible in certain thread states, specifically STA.
Take a look at this SO question for explanation.
So, write a static method like this:
static string GetClipboardText()
{
string result = string.Empty;
Thread staThread = new Thread(x =>
{
try
{
result = Clipboard.GetText();
}
catch (Exception ex)
{
result = ex.Message;
}
});
staThread.SetApartmentState(ApartmentState.STA);
staThread.Start();
staThread.Join();
return result;
}
and use it in your main method
oMail.TextBody = GetClipboardText();
I am writing a very data intensive Metro App where I need to send an email in html format. After googling around I came across this code.
var mailto = new Uri("mailto:?to=recipient#example.com&subject=The subject of an email&body=Hello from a Windows 8 Metro app.");
await Windows.System.Launcher.LaunchUriAsync(mailto);
This works great for me with one exception. I am generating the body of this email via html string so I have code in my class like this.
string htmlString=""
DALClient client = new DALClient();
htmlString += "<html><body>";
htmlString += "<table>";
List<People> people = client.getPeopleWithReservations();
foreach(People ppl in people)
{
htmlString+="<tr>"
htmlString +="<td>" + ppl.PersonName + "</td>";
htmlString +="</tr>";
}
htmlString +="</table>";
htmlString +="</body><html>";
Now when I run this code the email client opens up. However the result appears as plain text. Is there a way I can have this show up in the formatted html so the html tags and so forth wont display?
Thanks in advance.
It is just not possible to pass HTML to mailto. You could use sharing instead where you can pass HTML code to the default Windows Store Mail App (unfortunately not to the default Desktop Mail Application).
Here is a sample on a page:
public sealed partial class MainPage : Page
{
private string eMailSubject;
private string eMailHtmlText;
...
private void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
// Check if an email is there for sharing
if (String.IsNullOrEmpty(this.eMailHtmlText) == false)
{
// Pass the current subject
args.Request.Data.Properties.Title = this.eMailSubject;
// Pass the current email text
args.Request.Data.SetHtmlFormat(
HtmlFormatHelper.CreateHtmlFormat(this.eMailHtmlText));
// Delete the current subject and text to avoid multiple sharing
this.eMailSubject = null;
this.eMailHtmlText = null;
}
else
{
// Pass a text that reports nothing currently exists for sharing
args.Request.FailWithDisplayText("Currently there is no email for sharing");
}
}
...
// "Send" an email
this.eMailSubject = "Test";
this.eMailHtmlText = "Hey,<br/><br/> " +
"This is just a <b>test</b>.";
DataTransferManager.ShowShareUI();
Another approach would be to use SMTP but as far as I know there is no SMTP implementation yet for Windows Store Apps.
I would like to append a small message stating how the message to a user was sent when one chooses to use the SMSComposeTask or EmailComposeTask in my application. I would like it to say something like "sent from" + my application name but I am not sure how to navigate to the end of the user's message and then add my text. So far I've just navigated down a couple of lines and added it manually, but I would like to go to the next line and state the message to save the user from possibly sending multiple text messages once the 160 character limit is reached.
MainPage.xaml.cs
private void Messaging_Click(object sender, RoutedEventArgs e) {
SmsComposeTask smsComposeTask = new SmsComposeTask();
smsComposeTask.To = "";
smsComposeTask.Body = "Check out this application!" + "\n\n" + "sent from " + "QuickStarts";
smsComposeTask.Show();
}
private void Email_Click(object sender, RoutedEventArgs e) {
EmailComposeTask emailComposeTask = new EmailComposeTask();
emailComposeTask.Subject = "Share from " + "QuickStarts";
emailComposeTask.Body = "Check out this application!" + "\n\n" + "sent from " + "QuickStarts";
emailComposeTask.To = "";
emailComposeTask.Cc = "";
emailComposeTask.Bcc = "";
emailComposeTask.Show();
}
Once you've shown the SmsComposeTask or the EmailComposeTask, you have no control whichever on what the user will type. The best you can do is showing a textbox in your application so the user can type its message, then append your "sent from" text, doing every check you need, and only then show the task. But the user will still be able to change the message afterward.
When sending an e-mail using Microsoft Outlook I want to be able to send a hyperlink of file locations and websites in the body of the e-mail the body in my code is oMsg.Body. Any help would be greatly appreciated.
private void button13_Click(object sender, EventArgs e)
{
//Send Routing and Drawing to Dan
// Create the Outlook application by using inline initialization.
Outlook.Application oApp = new Outlook.Application();
//Create the new message by using the simplest approach.
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
//Add a recipient
Outlook.Recipient oRecip = (Outlook.Recipient)oMsg.Recipients.Add("email-address here");
oRecip.Resolve();
//Set the basic properties.
oMsg.Subject = textBox1.Text + " Job Release";
oMsg.Body = textBox1.Text + " is ready for release attached is the Print and Routing";
//Send the message. 6
oMsg.Send();
//Explicitly release objects.
oRecip = null;
oMsg = null;
oApp = null;
MessageBox.Show("Print and Routing Sent");
}
From MSDN, looks like you can set the BodyFormat to olFormatHTML and use the HTMLBody property:
oMsg.BodyFormat = olFormatHTML; // <-- Probably dont need this
oMsg.HTMLBody = "<HTML><BODY>Enter the message text here.</BODY></HTML>";
From the HTMLBody page, it looks like it sets the BodyFormat for you if you use the HTMLBody property, so you should be able to skip setting it.
Using HTML for the Body instead of plain text will allow you to include markup for hyperlinks:
oMsg.HTMLBody = "<html><body>";
oMsg.HTMLBody += textBox1.Text + " is ready for release attached is the Print and Routing";
oMsg.HTMLBody += "<p><a href='http://website.com'>Web Site</a></p></body></html>";
EDIT: Changed Body property to HTMLBody property.
I was wondering if it is possible through the .NET 2.0 MailMessage object to send an inline MHTML file that is created on the fly.
By inline I mean: It should be sent in a way that the user can see it, once he opens the email, without having to open/download the attachment.
It's a bit tricky, but yes, you can do it. In fact MailMessage class is nothing more than a wrapper above the system's CDO.Message class which can do the trick.
Also you can use AlternateView functionality, it's more simple:
MailMessage mailMessage = new MailMessage("me#me.com"
,"me#me.com"
,"test"
,"");
string ContentId = "wecandoit.jpg";
mailMessage.Body = "<img src=\"cid:" + ContentId + "\"/>";
AlternateView av = AlternateView.CreateAlternateViewFromString(mailMessage.Body
,null
,MediaTypeNames.Text.Html);
LinkedResource lr = new LinkedResource(#"d:\Personal\My Pictures\wecandoit.jpg");
lr.ContentId = ContentId;
lr.ContentType.Name = ContentId;
lr.ContentType.MediaType = "image/jpeg";
av.LinkedResources.Add(lr);
mailMessage.AlternateViews.Add(av);
SmtpClient cl = new SmtpClient();
cl.PickupDirectoryLocation = #"c:\test";
cl.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
cl.Send(mailMessage);
(jdecuyper -- thanks for the plug, as I wrote aspNetEmail).
You can do this with aspNetEmail. You can replace the entire contents of the email message with your MHT.
You can't do this with System.Net.Mail, but if you want to go the commerical route, pop me an email at dave#advancedintellect.com and I'll show you how this can be done.
If you want to go an open source route, there is probably some SMTP code on codeproject that you could modify to do this. Basically, you would inject your contents into the DATA command of the SMTP process.
One thing to note: If your MHT document has embedded scripts, flash, activeX objects or anything that may be blocked by the mail client, it probably won't render the same as what you are seeing in the browser.
Are you trying to add some images to an html email?
To accomplish this you will need to embed the images inside your email. I found a tutorial to accomplish it in a few lines of code. You can also buy the aspnetemail assembly. It has always helped me a lot to send emails with embedded images, they also have an excellent support team if anything goes wrong.
Keep in mind that embedding images makes your email heavier, but nicer :)
It is possible via CDO.Message (it is necessary add to project references COM library "Microsoft CDO for Windows 2000 Library"):
protected bool SendEmail(string emailFrom, string emailTo, string subject, string MHTmessage)
{
string smtpAddress = "smtp.email.com";
try
{
CDO.Message oMessage = new CDO.Message();
// set message
ADODB.Stream oStream = new ADODB.Stream();
oStream.Charset = "ascii";
oStream.Open();
oStream.WriteText(MHTmessage);
oMessage.DataSource.OpenObject(oStream, "_Stream");
// set configuration
ADODB.Fields oFields = oMessage.Configuration.Fields;
oFields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value = CDO.CdoSendUsing.cdoSendUsingPort;
oFields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value = smtpAddress;
oFields.Update();
// set other values
oMessage.MimeFormatted = true;
oMessage.Subject = subject;
oMessage.Sender = emailFrom;
oMessage.To = emailTo;
oMessage.Send();
}
catch (Exception ex)
{
// something wrong
}
}
It is possible via CDO.Message (it is necessary add to project references COM library "Microsoft CDO for Windows 2000 Library"):
protected bool SendEmail(string emailFrom, string emailTo, string subject, string MHTmessage)
{
string smtpAddress = "smtp.email.com";
try
{
CDO.Message oMessage = new CDO.Message();
// set message
ADODB.Stream oStream = new ADODB.Stream();
oStream.Charset = "ascii";
oStream.Open();
oStream.WriteText(MHTmessage);
oMessage.DataSource.OpenObject(oStream, "_Stream");
// set configuration
ADODB.Fields oFields = oMessage.Configuration.Fields;
oFields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value = CDO.CdoSendUsing.cdoSendUsingPort;
oFields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value = smtpAddress;
oFields.Update();
// set other values
oMessage.MimeFormatted = true;
oMessage.Subject = subject;
oMessage.Sender = emailFrom;
oMessage.To = emailTo;
oMessage.Send();
}
catch (Exception ex)
{
// something wrong
}
}