I am looking for a sample code on how to forward an existing email message (one that is already in my inbox) using the managed api.
When forwarded is there some way to keep a message original headers while forwarding it?
for example someone sent an email to me -i would like that ews will forward it to another recipient without changing the headers (original receive time from ,bcc etc...).
Given an EmailMessage object, you can just call the CreateForwareMessage() method:
var forwareMessage = item.CreateForward();
Regarding the other question: Get the MIME content of the mail and attach it to a new message:
item.Load(new PropertySet(BasePropertySet.IdOnly, ItemSchema.MimeContent));
var mail = new EmailMessage(service);
var attachment = mail.Attachments.AddFileAttachment("Original message.eml", item.MimeContent.Content);
attachment.ContentType = string.Format("message/rfc822; charset={0}", item.MimeContent.CharacterSet);
mail.ToRecipients.Add("hkrause#infinitec.de");
mail.Subject = "testmail";
mail.SendAndSaveCopy();
EDIT:
Create forward message and set reply to header:
var fw = item.CreateForward();
var fwMsg = fw.Save(WellKnownFolderName.Drafts);
fwMsg.ReplyTo.Add("personA#company.com");
fwMsg.SendAndSaveCopy();
Related
I have a bot that replies with a message consisting only of attachments. When it works on Slack, it uses Slack attachment formatting quite heavily, thus I have to use ChannelData property.
In version 1 of BotConnector, the code was like this
var reply = message.CreateReplyMessage();
reply.Attachments = new List<Attachment>();
var attachments = new List<object>(); //Slack-formatted attachments
//filling attachments...
reply.ChannelData = new {attachments};
and it worked. Now, in version 3 code has changed to
var reply = activity.CreateReply();
reply.Attachments = new List<Attachment>();
var attachments = new List<object>(); //Slack-formatted attachments
//filling attachments...
reply.ChannelData = new {attachments};
var connector = new ConnectorClient(new Uri(activity.ServiceUrl));
await connector.Conversations.ReplyToActivityAsync(reply);
which, basically, boils down to using different method to create reply, and another one to send the reply back.
Now, the problem is, that I don't get the reply back to Slack. Diagnostics in AppInsight show me that somewhere in Connector something like this happens:
Exception type: System.ArgumentNullException
Failed method: SlackChannel.SlackMapper+d__5.MoveNext
Exception Message: value cannot be null. Parameter name: source
ChannelData: {}
message: Invalid ChannelData
Please note that ChannelData in this diagnostics seems to be empty. So what I gather from all this is that something has changed in the way BotConnector processes ChannelData. How can I find out what exactly am I doing wrong?
Actually the problem is inside the ConnectorClient client, which strips channelData. The reason for that lies in its serialization settings which use ReadOnlyJsonContractResolver, which skips all read-only properties - and of course all the properties in anonymous class are read-only.
Knowing that, the solution is quite simple:
reply.ChannelData = JObject.FromObject(new {attachments});
Note the explicit use of JObject instead of anonymous class.
Have been facing an issue since days about EWS. So my scenario is;
I was to programmatically sync GMAIL and EXCHANGE ONLINE. So here is what I have done;
Connect to Gmail using Gmail API
Fetch mail from gmail get the email body, to, from, attachment and
all other thing
connect to Exchange online using EWS 2.0
Now the problem is here, how can I create an email in Inbox which looks like incoming mail from the sender;
Here is the code I have done;
_service = new ExchangeService(ExchangeVersion.Exchange2013);
_service.TraceEnabled = true;
_service.Credentials = new WebCredentials("admin#xyz.onmicrosoft.com", "password");
_service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
_service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "xyz#xyz.onmicrosoft.com");
EmailMessage message = new EmailMessage(_service);
Random r = new Random();
message.Subject = "Email Message";
message.From = new EmailAddress("xyz#gmail.com");
message.Sender = new EmailAddress("xyz#gmail.com");
message.Body = new MessageBody(BodyType.HTML, "<HTML><body><h1>This is a voice mail.</h1></BODY></HTML>");
message.ToRecipients.Add(new EmailAddress(""));
message.Save(WellKnownFolderName.Inbox);
This way its creating an email in inbox but it shows as a draft mail. I dont want it, I want it to look as RECEIVED mail.
Am I doing anything wrong?
You need to set a couple of properties before saving the message.
// Set a delivery time
ExtendedPropertyDefinition PidTagMessageDeliveryTime =
new ExtendedPropertyDefinition(0x0E06, MapiPropertyType.SystemTime);
DateTime deliveryTime = DateTime.Now; // Or whatever deliver time you want
message.SetExtendedProperty(PidTagMessageDeliveryTime, deliveryTime);
// Indicate that this email is not a draft. Otherwise, the email will appear as a
// draft to clients.
ExtendedPropertyDefinition PR_MESSAGE_FLAGS_msgflag_read = new ExtendedPropertyDefinition(3591, MapiPropertyType.Integer);
message.SetExtendedProperty(PR_MESSAGE_FLAGS_msgflag_read, 1);
These properties aren't settable after the items is saved, so it's important to do it before the first Save call.
I'm trying to send an Email via javamail adding embedded Images and an Outlook Invation which should be displayed showing up the buttons to accept/decline .... To make the buttons show up, i have to set the Content-Type of the MimeMultipart to related.
MimeMultipart multipart = new MimeMultipart("related");
Once this is done, the embedded Pictures wont show up again.
To Display the embedded images it should be alternative.
I have send myself a Mail from Outlook containing an Image as well as a invation to a meeting.
When I check the header there, it says:
Content-Type: multipart/related;
boundary="_005_CCF531A072806F489D02A6DD2CF322A01D88EEA2de35s004hst42wp_";
type="multipart/alternative"
I have already tried to set the Content-Type adding Parameters (type, charset ...) but it seems like their just getting ignored when sending the message. Here is the Method which I am calling to create the Message Part:
private Message generateMessage() throws Exception
{
Configuration conf = Configuration.getInstance();
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", conf.getProperty(Configuration.MAIL_SERVER));
// Get session
Session session = Session.getDefaultInstance(props, null);
// Create the message
Message message = new MimeMessage(session);
// Fill its headers
message.setSubject(this.subject);
message.setFrom(new InternetAddress(fromEmail));
Address[] replyTo = new Address[]{new InternetAddress(replyEmail)};
message.setReplyTo(replyTo);
// Create a related multi-part to combine the parts
MimeMultipart multipart = new MimeMultipart("related");
// Create your new message part
BodyPart messageBodyPart = new MimeBodyPart();
multipart.addBodyPart(messageBodyPart);
// ADD IMAGES
multipart = attachImages(multipart);
// ADD CALENDAR-FILE
multipart = attachCalendarFile(multipart);
// Set Content-Type
ContentType ct = new ContentType(message.getContentType());
ct.setParameter("charset", "utf-8");
ct.setParameter("type", "multipart/alternative");
// Set Multipart-Content to Message including the Content-Type
message.setContent(multipart, ct.toString());
return message;
}
The output of message.writeTo(System.out); is the following:
From: mail#sender.com
Reply-To: noreply#recipient.com
Message-ID: <1150322817.1.1397725164700.JavaMail.User#MacBooks-mailserver.local>
Subject: test
MIME-Version: 1.0
Content-Type: multipart/related;
boundary="----=_Part_0_72769750.1397725164655"
------=_Part_0_72769750.1397725164655
Is there any chance to make it work with embedded images AND invations?
Thanks in advance!
There's three types of multiparts, each used for a different purpose. In some cases you might need to use all three. For example, a message with a calendar attachment, html content using images, and equivalent plain text content, might have a structure such as this:
multipart/mixed
multipart/alternative
text/plain
multipart/related
text/html
image/jpeg
text/calendar
Google search led me here, so for future searchers:
#bill-shannon is of course correct. However, please save yourself the trouble of figuring out the low level Mime RFC stuff, and just use something like Simple Java Mail (open source) which figures out which structure suits best based on the input you provide. You can interactively explore all the possible structures Simple Java Mail can come up with. Disclaimer: I maintain this library (but I actually came across this SO question in my research into including voting buttons in emails for Outlook :) )
In your case you would use the following. First produce a Calendar content string, using something like ical4j:
Calendar icsCalendar = new Calendar();
icsCalendar.getProperties().add(new ProdId("-//Events Calendar//iCal4j 1.0//EN"));
icsCalendar.getProperties().add(Version.VERSION_2_0);
(..) // add attendees, organizer, end/start date and whatever else you need
// Produce calendar string
ByteArrayOutputStream bOutStream = new ByteArrayOutputStream();
new CalendarOutputter().output(icsCalendar, bOutStream);
String yourICalEventString = bOutStream.toString("UTF-8")
Then use Simple Java Mail's email builder to set the Calendar content alongside everything else such as images:
Email email = EmailBuilder
.startingBlank()
.withCalendarText(CalendarMethod.REQUEST, yourICalEventString)
.everythingElseLikeImagesHeadersAttachmentsEtcEtc()
.buildEmail();
Then send using the mailer builder:
MailerBuilder
.withSMTPServer("server", 25, "username", "password")
.buildMailer()
.sendMail(email);
Then depending on whether you provided text, html attachments or other things, Simple Java Mail will select the appropriate Mime structure with mixed/related/alternative parts.
I have an inbox set up in exchange, hello#mycompany.com
Additionally, there is an alias for this, news#mycompany.com, so all emails to the news address end up in the hello inbox.
Ideally, I want to be able to tell which alias an email has been sent to, using EWS.
When I send an email to news#mycompany.com, and examine the Internet headers of the message using Microsoft Outlook, the To: header reads To: Hello <news#mycompany.com> which is exactly what I want to see.
However, using EWS, when I look at the ToRecipients property of the message, the reported email address is always that of the primary SMTP address. Also the InternetMessageHeaders property of the Webservices.Data.Item does not contain the To: property. I also can't seem to see the correct address using EWSEditor to examine all the properties of the message.
The answer to this forum post seems to suggest that,
...The Information about the actual email address a message is sent to is stored in the recipients collection which you can't access (outside of exportmessage) in EWS...
How would I go about doing this programatically so I can find the correct To: address?
This works for me:
private static string GetToAddress()
{
ExchangeService exService = new ExchangeService();
exService.Credentials = new NetworkCredential("username", "password", "domain");
exService.Url = new Uri("https://youraddress/EWS/Exchange.asmx");
ExtendedPropertyDefinition PR_TRANSPORT_MESSAGE_HEADERS = new ExtendedPropertyDefinition(0x007D,MapiPropertyType.String);
PropertySet psPropSet = new PropertySet(BasePropertySet.FirstClassProperties)
{PR_TRANSPORT_MESSAGE_HEADERS, ItemSchema.MimeContent};
FindItemsResults<Item> fiResults = exService.FindItems(WellKnownFolderName.Inbox, new ItemView(1));
foreach (Item itItem in fiResults.Items)
{
itItem.Load(psPropSet);
Object valHeaders;
if (itItem.TryGetProperty(PR_TRANSPORT_MESSAGE_HEADERS, out valHeaders))
{
Regex regex = new Regex(#"To:.*<(.+)>");
Match match = regex.Match(valHeaders.ToString());
if (match.Groups.Count == 2)
return match.Groups[1].Value;
}
return ToAddress;
}
return "Cannot find ToAddress";
}
The code is from:
http://social.technet.microsoft.com/Forums/en-au/exchangesvrdevelopment/thread/1e5bbde0-218e-466e-afcc-cb60bc2ba692
I'm using the below method to reply to mails coming in to a business function mailbox.
The body text being added is only intermittently being set. This method is only called when someone has emailed in to unsubscribe from a mailing but the email address of the sender (or in the body) hasn't been found in the database and we want to ask them to send us the mail address they want to unsubscribe.
private void replyToMail(OutlookItem item)
{
RDOSession session = new RDOSession();
session.Logon(null, null, null, true, null, null);
RDOMail thisItem = session.GetMessageFromID(item.EntryID, item.StoreID, null);
RDOMail reply = thisItem.Reply();
RDOAddressEntry optingout = session.AddressBook.GAL.ResolveName("optingout");
//reply.Sender = optingout; this had no effect
reply.SentOnBehalfOf = optingout;
reply.Subject = "Automated Response - Could not complete unsubscribe";
reply.Body = "This is an automated response from the Newsletter unsubscribe system. We couldn't find "+item.Sender+" in our database to unsubscribe you from our mailings.\r\n\r\nPlease reply to this mail and include the email address you want to unsubscribe.\r\n\r\nKind Regards\r\n.";
reply.Send();
session.Logoff();
}
Firstly, if you are already usign OOM, there is no reason to call RDOSession.Logon. You can simply ste the MAPIOBJECT property:
Replace the line session.Logon() with
session.MAPIOBJECT = item.Application.Session.MAPIOBJECT
do not call Logoff.
Secondly, do yo umean the message is received with out a body? Do you see teh empty bofy in the Sent Items folder?
I had to edit thingie.HTMLBody as well as thingie.Body.
I suppose I could have figured out how to tell when to set the value of each one but since I just want to be sure that I have control of the body in this instance I'm simply setting both.