I want to create a new mail in the default mail app using UWP. Therefore I've written the following code:
List<string> emailAddresses = new List<string>();
public MainPage()
{
this.InitializeComponent();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
emailAddresses = new List<string>();
emailAddresses.Add("xx1#yyy.zzz");
emailAddresses.Add("xx2#yyy.zzz");
await ComposeEmail();
}
async Task ComposeEmail()
{
var emailMessage = new Windows.ApplicationModel.Email.EmailMessage();
foreach (string mailadress in emailAddresses)
{
var emailRecipient = new Windows.ApplicationModel.Email.EmailRecipient(mailadress);
emailMessage.To.Add(emailRecipient);
}
await Windows.ApplicationModel.Email.EmailManager.ShowComposeNewEmailAsync(emailMessage);
}
But the recipients in the mail which is created by this code are comma separated. If I try to send this mail with MS Outlook I'll get the message that I have to insert the recepients semicolon separated.
But I don't find a way to change this. I've also searched with Google - but I haven't found anything.
Does anyone know how I can solve this?
Thank you in forward.
Best regards
Matthias
Related
I just started using the open-source library called IMAPX to interact with my IMAP mailbox. I am following this article on CodeProject. I can login properly and retrieve the email folders. But the problem is, the article seems to be incomplete which is leaving me in the middle of the road. Firstly the Retrieving Email Folder's part didn't work. I had to do a workaround.Now, I am trying to download the emails of a folder.The article, regarding this issue, has only a few line of code:
private void foldersList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = foldersList.SelectedItem as EmailFolder;
if(item != null)
{
// Load the folder for its messages.
loadFolder(item.Title);
}
}
private void loadFolder(string name)
{
ContentFrame.Content = new FolderMessagesPage(name);
}
The article doesn't explain anything about FolderMessagesPage . So, I made a test page named FolderMessagesPage. I literally have no idea what to put in that page. Can anybody please guide me?
Unfortunately now I'm having some problems in accessing the article on Code Project, but if you need to retrieve the emails, you can start with the following sample code which retrieves the emails from the Inbox folder. I think that might work for you as well.
private static readonly ImapClient _client = new ImapX.ImapClient(ServerImapName, ImapPort, ImapProtocol, false);
if (!_client.Connect())
{
throw new Exception("Error on conncting to the Email server.");
}
if (!_client.Login(User, Password))
{
throw new Exception("Impossible to login to the Email server.");
}
public static List<string> GetInboxEmails()
{
var lstInEmails = new List<string>();
// select the inbox folder
Folder inbox = _client.Folders.Inbox;
if (inbox.Exists > 0)
{
var arrMsg = inbox.Search("ALL", ImapX.Enums.MessageFetchMode.Full);
foreach (var msg in arrMsg)
{
var subject = msg.Subject;
var mailBody = msg.Body.HasHtml ? msg.Body.Html : msg.Body.Text;
lstInEmails.Add(string.Concat(subject, " - ", mailBody );
}
}
return lstInEmails;
}
Hope it helps.
Good bytes.
I'm using HttpClient to POST a user created string to a server API and get the result. Previously I used a TextBox to allow the user to enter the string but I wanted pretty colours so I tried to replace the TextBox with a RichEditBox but failed miserably. When using the TextBox everything worked fine but when using the RichEditBox I get an "Access denied" message. I get the text from the RichEditBox with the Document.GetText method. I can get it to work by either inserting a static string in the place where I get the text or in the place where I send the string into a FormUrlEncodedContent. The string is edited before and after adding the text from the RichEditBox, and sent to another method.
TL:DR: Sending a string from a TextBox with a HttpClient using POST works but not when replacing the TextBox with a RichEditBox.
Here's the full error message:
System.UnauthorizedAccessException: 'Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))'
Does anyone have a solution or an explanation to why this is happening?
Edit
Code sample:
private async void RichEditBox_KeyUp(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Enter)
{
await RunCode(); //The error points to this line
}
}
private async void RunCode()
{
string code = CodeBefore;
foreach (RichEditBox tb in editStack.Children) //If I comment out the foreach loop I don't get any errors
{
if (tb.Tag.ToString() == "input")
{
tb.Document.GetText(Windows.UI.Text.TextGetOptions.None, out string thisLine);
code += thisLine;
}
}
code += CodeAfter;
await RunCSharp(code);
}
private async Task<Code> RunCSharp(string code)
{
Code re = new Code();
using (HttpClient client = new HttpClient())
{
FormUrlEncodedContent content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("LanguageChoiceWrapper", "1"),
new KeyValuePair<string, string>("Program", code), //If I replace code with a string I don't get any errors
new KeyValuePair<string, string>("ShowWarnings", "false")
});
try
{
HttpResponseMessage msg = await client.PostAsync("http://mywebaddress.com/run", content);
re = JsonConvert.DeserializeObject<Code>(await msg.Content.ReadAsStringAsync());
}
catch { }
}
return re;
}
So I have this button in my Portable Class Library that should directly attach an xml file I've created into a mail and send it using the Messaging PlugIn. The problem is that .WithAttachment() is not supported in PCL so I wanted to ask if I can get around this using DependencyService and if so, how?
Can I just return .WithAttachment() from the UWP class (as UWP is my target platform)? Wouldn't there be a conflict because I've read that the overload of .WithAttachment() in UWP is .WithAttachment(IStorageFile file).
private void Senden_Clicked(object sender, EventArgs e)
{
var emailMessenger = CrossMessaging.Current.EmailMessenger;
if (emailMessenger.CanSendEmail)
{
var email = new EmailMessageBuilder()
.To("my.address#gmail.com")
.Subject("Test")
.Body("Hello there!")
//.WithAttachment(String FilePath, string ContentType) overload showing in PCL
//.WithAttachment(IStorageFile file) overload for the UWP according to the documentation
.Build();
emailMessenger.SendEmail(email);
}
}
EDIT:
I've been able to modify Nick Zhou's answer a little bit to be able to send an email with attachment via button-click. I just changed this peace of code:
var picker = new Windows.Storage.Pickers.FileOpenPicker
{
ViewMode = Windows.Storage.Pickers.PickerViewMode.List,
SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary
};
picker.FileTypeFilter.Add("*");
to this:
StorageFolder sf = ApplicationData.Current.LocalFolder;
var file = await sf.GetFileAsync("daten.xml");
Of course you then need to create the file inside the app's local folder instead of the documents library.
The problem is that .WithAttachment() is not supported in PCL so I wanted to ask if I can get around this using DependencyService and if so, how?
Of course you can use DependencyService to achieve sending email with attactment. But you could create two interface like code behind.
SendEmail Interface
public interface IMessageEmail
{
void SendEmailMehod(string address, string subject, string body, StorageFile attactment = null);
}
IMessageEmail implementation in UWP project.
public void SendEmailMehod(string address, string subject, string body, StorageFile attactment = null)
{
var emailMessenger = CrossMessaging.Current.EmailMessenger;
if (emailMessenger.CanSendEmail)
{
if (attactment != null)
{
var email = new EmailMessageBuilder()
.To(address)
.Subject(subject)
.Body(body)
.WithAttachment(attactment)
.Build();
emailMessenger.SendEmail(email);
}
else
{
var email = new EmailMessageBuilder()
.To(address)
.Subject(subject)
.Body(body)
.Build();
emailMessenger.SendEmail(email);
}
}
}
As you can see the .WithAttachment(attactment) parameter is IStorageFile. So you need pass a file to the method. Hence you could create another DependencyService.
IFilePicker Interface
public interface IFilePicker
{
Task<StorageFile> getFileAsync();
}
IMessageEmail implementation in UWP project.
public async Task<StorageFile> getFileAsync()
{
var picker = new Windows.Storage.Pickers.FileOpenPicker
{
ViewMode = Windows.Storage.Pickers.PickerViewMode.List,
SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary
};
picker.FileTypeFilter.Add("*");
var file = await picker.PickSingleFileAsync();
if (file != null)
{
return file;
}
return null;
}
You can try the project I have upload to github.
I have a need to find a particular email on a Google IMAP server and then save the attachments from the email. I think I have it all figured out except for the part of determining what the file name is of the attachment.
Below is my code so far, I am hoping that someone can point me in the right direction to determine the file name.
I have Googled and SO'd but have not been able to find something using the attachment approach.
internal class MailKitHelper
{
private void SaveAttachementsForMessage(string aMessageId)
{
ImapClient imapClient = new ImapClient();
imapClient.Connect("imap.google.com", 993, SecureSocketOptions.Auto);
imapClient.Authenticate("xxxx", "xxxx");
HeaderSearchQuery searchCondition = SearchQuery.HeaderContains("Message-Id", aMessageId);
imapClient.Inbox.Open(FolderAccess.ReadOnly);
IList<UniqueId> ids = imapClient.Inbox.Search(searchCondition);
foreach (UniqueId uniqueId in ids)
{
MimeMessage message = imapClient.Inbox.GetMessage(uniqueId);
foreach (MimeEntity attachment in message.Attachments)
{
attachment.WriteTo("WhatIsTheFileName"); //How do I determine the file name
}
}
}
}
And the winner is.....
attachment.ContentDisposition.FileName
Question
How would I look at the attachment properties using EWS (Exchange 2013, C#) and retrieve the original sender's email address? Not the email address of the current sender, but the sender of the email that is attached to this email.
What I Did
Lots of googling has shown me only how to retrieve the sender of the current email and not the attachement. I do this by
//get sender of email to TR
EmailMessage mes = (EmailMessage)item;
String sender = mes.Sender.Address;
Request
Thoughts? Links? Sample code? I am looking for anything right now that I can use to help me load up the attachment and pull the sender email address. Thanks!
You want to fetch details on attached emails right?
Try This Code Snippet? Assuming _ewsService is a correctly bound service client.
var results = _ewsService.FindItems(WellKnownFolderName.Inbox, new ItemView(100)); //fetch 100 random emails from inbox
foreach (var entry in results.Items)
{
if (entry is EmailMessage)
{
var temp = EmailMessage.Bind(_service, entry.Id);
if (entry.HasAttachments)
{
temp.Load(new PropertySet(EmailMessageSchema.Attachments));
foreach (var singleItem in temp.Attachments)
{
if (singleItem is ItemAttachment)
{
var attachedMail = singleItem as ItemAttachment;
attachedMail.Load();
Console.WriteLine(attachedMail.Item is EmailMessage);
var workingMessage = attachedMail.Item as EmailMessage; //this should give you from, subject, body etc etc.
}
}
}
}
}