我正在开发这个从交换Web服务中提取数据的Windows服务。一切都像魅力,但我无法获得邮件附件的contentType / Mime类型。
有人能指出我找到问题的方向吗?
protected override void OnStart(string[] args)
{
Svc = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
Svc.Credentials = new WebCredentials("kbatest@xxxx.xx", "kba123test");
Svc.TraceEnabled = true;
Svc.TraceFlags = TraceFlags.All;
Svc.AutodiscoverUrl("kbatest@xxxx.xx", RedirectionUrlValidationCallback);
streamsubscript = Svc.SubscribeToStreamingNotifications(new FolderId[] { WellKnownFolderName.Inbox }, EventType.NewMail);
connection = new StreamingSubscriptionConnection(Svc, 30);
connection.AddSubscription(streamsubscript);
connection.OnNotificationEvent += OnNotificationEvent;
connection.OnSubscriptionError += ConnectionOnOnSubscriptionError;
connection.OnDisconnect += ConnectionOnOnDisconnect;
connection.Open();
}
private void OnNotificationEvent(object sender, NotificationEventArgs args)
{
foreach (var notification in args.Events)
{
if (notification.EventType != EventType.NewMail) continue;
var itemEvent = (ItemEvent)notification;
mail = GetMailByID(itemEvent.ItemId.UniqueId);
List<int> ls = ExtractTicketNumber(mail.Subject);
if (ls.Count > 0)
{
foreach (int i in ls)
{
bool exist = CheckTicketExistance(i);
if (exist)
{
SaveMailToEvent(mail, i);
}
}
}
else
{
SaveMailToTicket(mail);
}
}
}
private void SaveMailToEvent(EmailMessage mail, int ticketId)
{
string[] mailAddr = mail.From.Address.Split(new char[] { '@' });
string user = mailAddr[0].ToUpper();
var eventId = 0;
using (var conn = new SqlConnection(connectionStr))
{
conn.Open();
eventId = conn.Query<int>("INSERT INTO THGITTicketEvents (EventStatus, CreatedBy, Comment, RefTicketRecID, FileAttached)" +
"Values(@EventStatus, @CreatedBy, @Comment, @RefTicketRecID, @FileAttached)" +
"SELECT CAST( @@IDENTITY AS int)",
new
{
EventStatus = 9,
CreatedBy = user,
Comment = mail.Body.Text,
RefTicketRecID = ticketId,
FileAttached = (mail.HasAttachments) ? 1 : 0
}).Single();
}
if (mail.HasAttachments)
{
using (var conn = new SqlConnection(connectionStr))
{
conn.Open();
foreach (var i in mail.Attachments)
{
FileAttachment fileAttachment = i as FileAttachment;
if (fileAttachment != null)
{
fileAttachment.Load();
string test = fileAttachment.ContentType;
conn.Query("INSERT INTO THGITAttachments (FileContent, CreatedBy, FileName, RefEventRecID, MimeType)" +
"Values(@FileContent, @CreatedBy, @FileName, @RefEventRecID, @MimeType)",
new
{
FileContent = fileAttachment.Content,
CreatedBy = user,
FileName = fileAttachment.Name,
RefEventRecID = eventId,
MimeType = fileAttachment.ContentType,
});
}
}
}
}
}