This problem is happening for one of our customers and I have been unable to replicate on my side using the same version of Outlook. My customer and I are using Office 365 with Outlook 2016 installed. When he sends an email in our program via Outlook Redemption (a third party program used for Outlook integration), the mail gets stuck in his outbox.
If he double clicks the message (so it pops up in Outlook) he can hit the send button and it will sucessfuly send. If they use an old version of Outlook (2010) this is not a problem. I upgraded them to the the newest version of Outlook Redmeption at the time (released May 07, 2016), though it looks like they just came out with a new version a few days ago. I'll try that soon, but the changelog doesn't mention mail getting stuck in the Outbox.
I also noticed that the emails in his outbox have what appears to be the 'draft' symbol on them, while in my outbox they have a 'sending' symbol on them. This seems important, but I'm not sure what I can do about that.
Also, hitting Send/Receive All Folders does not help.
My code is below. Thank you for any assistance.
public static bool SendMessage(Recipients recipients, string[] addressListReplyTo, string subject, string body, string[] attachments, bool requestReadReceipt, Log log, bool isHtmlBody = false)
{
RDOSession session = null;
RDOMail mail;
RDOFolder folder;
bool result = true;
session = GetSessionAndLogon(log);
if (session == null)
return false;
folder = session.GetDefaultFolder(rdoDefaultFolders.olFolderOutbox);
mail = folder.Items.Add();
if (isHtmlBody)
mail.HTMLBody = body;
else
mail.Body = body;
mail.Subject = subject;
mail.ReadReceiptRequested = requestReadReceipt;
foreach (string attachment in attachments)
{
if (attachment != "")
mail.Attachments.Add(attachment);
}
foreach (string address in addressListReplyTo)
{
if (address != "")
mail.ReplyRecipients.Add(address);
}
foreach (string address in recipients.To)
{
if (address != "")
mail.Recipients.Add(address).Type = 1;
}
foreach (string address in recipients.Cc)
{
if (address != "")
mail.Recipients.Add(address).Type = 2;
}
foreach (string address in recipients.Bcc)
{
if (address != "")
mail.Recipients.Add(address).Type = 3;
}
foreach (RDORecipient recipient in mail.Recipients)
{
if (!OutlookMailEngine64.existsName(recipient.Name, session, log == null ? null : log))
result = false;
}
if (result)
{
try
{
mail.Send();
result = true;
}
catch (System.Runtime.InteropServices.COMException ex)
{
string message = "Error while sending email: " + ex.Message;
if (log != null)
log.Message(message);
if (OutlookMailEngine64.DiagnosticMode)
MessageBox.Show(message);
throw new EmailLibraryException(EmailLibraryException.ErrorType.InvalidRecipient, "One or more recipients are invalid (use OutlookMailEngine64.ValidateAddresses first)", ex);
}
}
if (session.LoggedOn)
session.Logoff();
return result;
}
Keep in mind that message submission is asynchronous, and it will nto be automatically triggered unless you are using the online Exchange store (where store and transport provider are tightly coupled).
You can force send/receive by calling Namespace.SendAndReceive in the Outlook Object Model.
Dmitry worked with me via email. The solution for me was to swap out RDO for the SafeMailItem object. Here is the updated version of my method so you can see the changes:
private static bool SendSafeMessage(Recipients recipients, string[] addressListReplyTo, string subject, string body, string[] attachments, bool requestReadReceipt, Log log, bool isHtmlBody = false)
{
//This method was added because sometimes messages were getting stuck in the Outlook Outbox and this seems to solve that
bool result = true;
Microsoft.Office.Interop.Outlook.Application application = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.NameSpace namespaceMAPI = application.GetNamespace("MAPI");
namespaceMAPI.Logon();
RDOSession session = null;
session = GetSessionAndLogon(log); //TODO: I'm creating a 2nd session here which is wasteful
SafeMailItem safeMail = Redemption.RedemptionLoader.new_SafeMailItem();
Microsoft.Office.Interop.Outlook.MailItem outlookMailItem = (Microsoft.Office.Interop.Outlook.MailItem)application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
safeMail.Item = outlookMailItem;
if (isHtmlBody)
outlookMailItem.HTMLBody = body;
else
safeMail.Body = body;
outlookMailItem.Subject = subject;
outlookMailItem.ReadReceiptRequested = requestReadReceipt;
foreach (string attachment in attachments)
{
if (attachment != "")
safeMail.Attachments.Add(attachment);
}
foreach (string address in addressListReplyTo)
{
if (address != "")
safeMail.ReplyRecipients.Add(address);
}
foreach (string address in recipients.To)
{
if (address != "")
safeMail.Recipients.Add(address).Type = 1;
}
foreach (string address in recipients.Cc)
{
if (address != "")
safeMail.Recipients.Add(address).Type = 2;
}
foreach (string address in recipients.Bcc)
{
if (address != "")
safeMail.Recipients.Add(address).Type = 3;
}
foreach (Microsoft.Office.Interop.Outlook.Recipient recipient in outlookMailItem.Recipients)
{
if (!OutlookMailEngine64.existsName(recipient.Name, session, log == null ? null : log))
result = false;
}
if (result)
{
try
{
safeMail.Send();
result = true;
}
catch (System.Runtime.InteropServices.COMException ex)
{
string message = "Error while sending email: " + ex.Message;
if (log != null)
log.Message(message);
if (OutlookMailEngine64.DiagnosticMode)
MessageBox.Show(message);
throw new EmailLibraryException(EmailLibraryException.ErrorType.InvalidRecipient, "One or more recipients are invalid (use OutlookMailEngine64.ValidateAddresses first)", ex);
}
}
if (session.LoggedOn)
session.Logoff();
namespaceMAPI.Logoff();
return result;
}
Related
ic = new ImapClient("imap.yandex.com", Email, email.Password, AuthMethods.Login, 993, true);
var mailMessage = ic.SearchMessages(SearchCondition.From("somemail#gmail.com"), false, true).ToList();
if (mailMessage.Count > 0)
{
foreach (Lazy<AE.Net.Mail.MailMessage> Lmail in mailMessage)
{
AE.Net.Mail.MailMessage mail = Lmail.Value;
//SendSmtpMail(mail.Subject, ReplyData, Email, Password, mail.From,mail.MessageID);
if (mail != null)
{
if (mail.To.FirstOrDefault(x => x.Equals(email.EmailName)) != null)
{
if (mail.Body.IndexOf("Attachment Name:", StringComparison.OrdinalIgnoreCase) > -1)
{
Code= Between(mail.Body, "to:", "\r");
}
else
{
_msg.Add("CODE NOT FOUND..." + Environment.NewLine);
}
}
}
}
}
I'm using AE Mail plugin from nuget. Problem is, the following line:
AE.Net.Mail.MailMessage mail = Lmail.Value;
took almost 1 second for lazy enumeration. This is so bothering when parsing lots of mails. Any suggestions?
I want to send EMails with C# and the Microsoft.Office.Interop.Outlook library.
I am locked in, on my Outlook account with a#domain.com and got the rights on the exchange server to send mails from b#domain.com and c#domain.com.
I don't find any way to send a mail with B or C.
I got everything working for my user account which has the mail a#domain.com.
Now I can't find a way to get the account of b#domain.com and send a mail in the name of B.
My Question:
How do I get access to the other accounts?
That's the code I have:
using Outlook = Microsoft.Office.Interop.Outlook;
public static Outlook.Account GetAccountForEmailAddress(Outlook.Application application, string smtpAddress)
{
// Loop over the Accounts collection of the current Outlook session.
Outlook.Accounts accounts = application.Session.Accounts;
if (_IsDebug)
Console.WriteLine($"Anzahl Accounts: {accounts.Count}");
foreach (Outlook.Account account in accounts)
{
// When the e-mail address matches, return the account.
if (_IsDebug)
Console.WriteLine($"Account: {account.SmtpAddress}");
if (String.Compare(account.SmtpAddress, smtpAddress, true) == 0)
{
return account;
}
}
throw new System.Exception(string.Format("No Account with SmtpAddress: {0} exists!", smtpAddress));
}
static void SendEMail(string emailadress)
{
try
{
var outlookApplication = new Outlook.Application();
var outlookMailItem = (Outlook.MailItem)outlookApplication.CreateItem(Outlook.OlItemType.olMailItem);
outlookMailItem.SendUsingAccount = GetAccountForEmailAddress(outlookApplication, ConfigurationManager.AppSettings[SENDER]);
if (_IsDebug)
Console.WriteLine($"Absender: {outlookMailItem?.SendUsingAccount?.SmtpAddress}");
outlookMailItem.HTMLBody = ConfigurationManager.AppSettings[BODY];
if (_IsDebug)
Console.WriteLine($"Body: {outlookMailItem?.HTMLBody}");
var file = GetPDFFile();
if (_IsDebug)
Console.WriteLine($"File: {file?.Name}");
if (file == null)
{
Console.WriteLine("Keine Datei gefunden!");
return;
}
string attachementDisplayName = file.Name;
int attachementPosition = outlookMailItem.HTMLBody.Length + 1;
int attachementType = (int)Outlook.OlAttachmentType.olByValue;
if (_IsDebug)
Console.WriteLine($"Dateianhang: {file.FullName}");
Outlook.Attachment outlookAttachement = outlookMailItem.Attachments.Add(file.FullName, attachementType, attachementPosition, attachementDisplayName);
outlookMailItem.Subject = ConfigurationManager.AppSettings[SUBJECT];
Outlook.Recipients outlookRecipients = outlookMailItem.Recipients;
Outlook.Recipient outlookRecipient = outlookRecipients.Add(emailadress);
outlookRecipient.Resolve();
outlookMailItem.Send();
outlookRecipient = null;
outlookRecipients = null;
outlookMailItem = null;
outlookApplication = null;
if (_IsDebug)
Console.ReadLine();
}
catch (Exception)
{
throw;
}
}
You have to grant the owner of the sending mailbox Send As permissions on the mailboxes you want to send the emails from. Send on behalf of permissions will work but will say "A on behalf of B" as the sender.
i'm using Outlook Interop and C# to get some mail information to create an excel report. I have email items list from Sent Items folder and i want to get the email address who i sent the mail. I found the "To" property but only return the person name not the address.
Anyone can please help me to return the email addres from Outlook.MailItem object?
You can ask me if you need more information. Thanks!!!!
Here is my code where i am setting the properties:
foreach (object mail in mails)
//mails is a list from Sent Items folder
{
if (mail is Outlook.MailItem)
{
var item = (Outlook.MailItem)mail;
//i need the address in provider email
var providerEmail = someProperty(????);
var name = item.To;
var request= "Other Request";
var emailDate= item.ReceivedTime;
var status = "Closed";
var responseDate= item.CreationTime;
var reportObject = new ReportModel
{
Email = providerEmail ,
Name = name,
Solicitud = request,
EmailDate = emailDate,
Status = status,
ResponseDate = responseDate
};
}
}
MailItem has Recipients property, you can use it to obtain recipient of each type:
To
Cc
Bcc.
Use recipient.Type to recognize recipient type and recipient.Address to obtain its email address.
Example:
protected override void getRecepients(MailItem OLitem, StringBuilder toStringBuilder,
StringBuilder ccStringBuilder, StringBuilder bccStringBuilder)
{
try
{
var recipients = OLitem.Recipients;
string parent = string.Empty;
foreach (Microsoft.Office.Interop.Outlook.Recipient recipient in recipients)
{
switch (recipient.Type)
{
case (int)Microsoft.Office.Interop.Outlook.OlMailRecipientType.olTo:
toStringBuilder.Append(recipient.Address + ", ");
if (parent == string.Empty)
{
parent = recipient.Address;
}
break;
case (int)Microsoft.Office.Interop.Outlook.OlMailRecipientType.olCC:
ccStringBuilder.Append(recipient.Address + ", ");
break;
case (int)Microsoft.Office.Interop.Outlook.OlMailRecipientType.olBCC:
bccStringBuilder.Append(recipient.Address + ", ");
break;
default:
break;
}
}
}
catch (Exception ex)
{
// do something with error
}
}
We have the below code to read the attachments using EWS.
FindItemsResults<Item> foundItems = service.FindItems(FolderId, new ItemView(1000));
var orderItems = from list in foundItems
orderby list.DateTimeReceived
select list;
foreach (EmailMessage item in orderItems)
{
item.Load();//SARANYA
EmailMessage foundEmail = (EmailMessage)item;
EmailMessage message = EmailMessage.Bind(service, new ItemId(item.Id.ToString()), new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.Attachments, ItemSchema.Body));
if (message.Attachments.Count > 0)
{
FileAttachment[] attachments = null;
attachments = new FileAttachment[message.Attachments.Count];
foreach (Attachment attachment in message.Attachments)
{
try
{
if (attachment is FileAttachment)
{
FileAttachment fileAttachment = attachment as FileAttachment;
// System.Threading.Thread.Sleep(2000);
fileAttachment.Load();
byte[] FolloupMailFileAttachmentContentBytes = fileAttachment.Content;
bool isScreenshot = false;
string ScreenfileName = "";
for (int i = 0; i < imgSrcs.Count; i++)
{
if (imgSrcs[i].ToString() == fileAttachment.Name.ToString())
{
isScreenshot = true;
if (!imgSrcs[i].ToString().Contains(".png"))
ScreenfileName = "cid:" + imgSrcs[i].ToString() + ".png";
else
ScreenfileName = "cid:" + imgSrcs[i].ToString();
break;
}
}
if (FolloupMailFileAttachmentContentBytes != null)
{
if (isScreenshot && RemoveSuccess == 1)
{
InsertMailItemAttachment(ScreenfileName, FolloupMailFileAttachmentContentBytes, caseid);
}
else
InsertMailItemAttachment(fileAttachment.Name.ToString(), FolloupMailFileAttachmentContentBytes, caseid);
}
}
else if (attachment is ItemAttachment)
{
item.Move(unreadmailFolder.Id);
}
}
catch (Exception exe)
{
if (!ReadMoved)
{
item.Move(readmailFolder.Id);
ReadMoved = true;
}
logfile.HandleError(exe, "Attachment Exception \n\nEmailbox - " + EMailBox + "\n\nEmail Subject - " + strSubject + " \n - Could not load the attachment (" + attachment.Name.ToString() + ")");
}
}
}
}
Above code is working when I provide thread.sleep() before fileattachment.load(). when thread.sleep is removed, I get the below exception.
Error Source : Microsoft.Exchange.WebServices
Target Site : Void InternalThrowIfNecessary()
System Message : The specified object was not found in the store.
Stack Trace : at Microsoft.Exchange.WebServices.Data.ServiceResponse.InternalThrowIfNecessary()
at Microsoft.Exchange.WebServices.Data.ServiceResponse.ThrowIfNecessary()
at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute()
at Microsoft.Exchange.WebServices.Data.ExchangeService.InternalGetAttachments(IEnumerable`1 attachments, Nullable`1 bodyType, IEnumerable`1 additionalProperties, ServiceErrorHandling errorHandling)
at Microsoft.Exchange.WebServices.Data.ExchangeService.GetAttachment(Attachment attachment, Nullable`1 bodyType, IEnumerable`1 additionalProperties)
at Microsoft.Exchange.WebServices.Data.Attachment.InternalLoad(Nullable`1 bodyType, IEnumerable`1 additionalProperties)
at Microsoft.Exchange.WebServices.Data.Attachment.Load()
at EMT_Office365_MailFetch_Scheduler.Program.FindEmail(Object threadState) in
Experts, Please provide your valuable inputs
Your logic doesn't look correct eg
item.Move(unreadmailFolder.Id);
Should not be inside the Foreach loop if you want to move the message at the end just use a Flag and process it once you have finished processing the attachments(you logic doesn't work if you have two ItemAttachment this would execute twice). The reason sleep is working is mostly likely an Aysnc operation is completing once you have executed the move. Thats why this should be outside of the foreach attachment loop
I am developing an outlook add-in using add-in express. When I get a outbox mail item, I can see my email address under "mailItem.SenderEmailAddress" as in x500 format. Is there a way to convert it as a SMTP email address.
Here is my code :
Outlook.Explorer expl = null;
Outlook.Selection selection = null;
Outlook.MailItem mailItem = null;
Outlook.Recipient recipient = null;
string recipientsEmail = "";
try
{
expl = Globals.ObjOutlook.ActiveExplorer() as Outlook.Explorer; ;
if (expl != null)
{
selection = expl.Selection;
if (selection.Count == 1)
{
if (selection[1] is Outlook.MailItem)
{
mailItem = (selection[1] as Outlook.MailItem);
if (mailItem != null)
{
string senderEmailAddress = mailItem.SenderEmailAddress;
What I have tried and Succeeded :- I know how to convert x500 type to SMTP using a Outlook.Recipient object. There I can get the "addressEntry" of the Recipient and obtain ExhangeUser, then go for the "PrimarySmtpAddress" of ExhangeUser. But I am not quiet sure about how to deal with SenderEmailAddress and convert it as SMTP.
Some interesting experiments :-
Since the property of "Sender" is not available in the current mailitem object, I managed to use reflection to get the property of "Sender". But When I run following code the "propertyInfo" object value is getting null. I can't understand why.
Here is the code
//...
if (mailItem != null)
{
var mailItem2 = GetNewObject(mailItem, "Sender", intArray);
//...
public static object GetNewObject(Outlook.MailItem o, string popertyName, object[] parameters)
{
try
{
PropertyInfo propertyInfo = o.GetType().GetProperty(popertyName); // This object is getting null
return propertyInfo.GetType().GetProperty(popertyName).GetValue(o,parameters) as Outlook.MailItem;
}
catch (MissingMethodException ex)
{
// discard or do something
Debug.DebugMessage(2, "AddinModule : Error in GetNewObject() : " + ex.Message);
return null;
}
}
Please advice me. Thank you.
You can obtain the AddressEntry of the sender using the Sender property like this:
Outlook.AddressEntry senderAddressEntry = mailItem.Sender;
After reading the comment from "Dmitry Streblechenko" I was able to develop a fully working solution for my self. Here is the code
private void adxRibBtnAddEmailAddress_OnClick(object sender, IRibbonControl control, bool pressed)
{
Outlook.MailItem mailItem = null;
Outlook.Recipient recipient = null;
string recipientsEmail = "";
string sendersEmail = "";
try
{
mailItem = OutlookHelper.GetSelectedMailItem();
if (mailItem != null)
{
if (mailItem.SenderEmailType == "EX")
sendersEmail = GetSenderEmailAddress(mailItem);
else
sendersEmail = mailItem.SenderEmailAddress;
if (!string.IsNullOrEmpty(sendersEmail))
{
if (sendersEmail == Globals.OLCurrentUserEmail) // Sent mail
{
recipient = mailItem.Recipients[1];
if (recipient != null)
{
recipientsEmail = OutlookHelper.GetAddress(ref recipient);
if (!string.IsNullOrEmpty(recipientsEmail))
{
// Do Something
}
}
}
else // inbox mail
{
// Do Something
}
}
}
}
catch (Exception ex)
{
Debug.DebugMessage(2, "AddinModule : Error in adxRibBtnsddemailaddress_OnClick() : " + ex.Message);
}
finally
{
if (recipient != null) Marshal.ReleaseComObject(recipient);
if (mailItem != null) Marshal.ReleaseComObject(mailItem);
Cursor.Current = Cursors.Default;
}
}
private string GetSenderEmailAddress(Outlook.MailItem oM)
{
Outlook.PropertyAccessor oPA = null;
Outlook.AddressEntry oSender = null;
Outlook.ExchangeUser oExUser = null;
string SenderID;
string senderEmailAddress;
try
{
// Create an instance of PropertyAccessor
oPA = oM.PropertyAccessor;
// Obtain PidTagSenderEntryId and convert to string
SenderID = oPA.BinaryToString(oPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0C190102"));
// Obtain AddressEntry Object of the sender
oSender = Globals.ObjNS.GetAddressEntryFromID(SenderID);
oExUser = oSender.GetExchangeUser();
senderEmailAddress = oExUser.PrimarySmtpAddress;
return senderEmailAddress;
}
catch (Exception ex)
{
Debug.DebugMessage(2, "AddinModule : Error in adxRibBtnAddGenInteraction_OnClick() : " + ex.Message);
return null;
}
finally
{
if (oExUser != null) Marshal.ReleaseComObject(oExUser);
if (oSender != null) Marshal.ReleaseComObject(oSender);
if (oPA != null) Marshal.ReleaseComObject(oPA);
}
}