Error in EmailMessage.Forward (EWS) method. - c#

I'm using EWS to read a mailbox and based on a certain conditions, forward the emails out to a set of users. However, the process fails with this error:
"An internal server error occurred. The operation failed."
and this:
"An unhandled exception of type 'Microsoft.Exchange.WebServices.Data.ServiceResponseException' occurred in Microsoft.Exchange.WebServices.dll"
Here's my sample code:
EmailAddress[] emailids = new EmailAddress[4];
emailids[0] = new EmailAddress("user1#domain.com");
emailids[1] = new EmailAddress("user2#domain.com");
msg.Forward("This message was Auto forwarded", emailids);
The msg is an object of type EmailMessage in Microsoft.Exchange.WebServices.Data;
Any help would be appreciated.
Thank you
Abhi

In your code you've defined an array of 4 EmailAddress but you are only using 2 which would cause issues.
Does this fail on every message you try to forward or just certain messages or are you forwarding as batch and fails after a certain number.
There are Throttling restrictions with Exchange http://msdn.microsoft.com/en-us/library/office/jj945066(v=exchg.150).aspx that can affect a number of things one of those in the recipient limits eg RecipientRateLimit and ForwardeeLimit.
You might also want to enable tracing http://msdn.microsoft.com/en-us/library/office/dd633676(v=exchg.80).aspx and post the full response you get
Cheers
Glen

Related

Counting Emails in inbox from SSIS (C#) - The remote server returned an error: (401) Unauthorized

I am trying to create a connection to the ExchangeService from a script component (C#) in SSIS to be able to count the total number of emails in an inbox.
My error comes when I try to use the connection - so far I've been testing the connection to simply try and send a test email to myself and its on msg.send() that I get the error:
The remote server returned an error: (401) Unauthorized
I have been using the WebServices (EWS) NuGet Package with the code below:
ExchangeService exService = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
string username = "emailUser";
string pwd = "MyPwd";
string domain = "NetBIOS_DOMAIN";
exService.Credentials = new WebCredentials(username, pwd, domain);
exService.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
EmailMessage msg = new EmailMessage(exService)
{
Subject = "This is a test!!!",
Body = "This is the message body"
};
msg.ToRecipients.Add("emailUser#domain.co.uk");
msg.Send();
I've not got as far as looking into inboxes yet because as soon as I try to bind a folder I get the same error so figured this was an easier test.
I have tried both WebCredentials() and NetworkCredential() - both give the same error.
I have been primarily following the notes from here
A search on the error led me to try all manor of variances with the username/domain as outlined in an answer here
And I have tested my remote connectivity with this tool which gives the same error (Screenshot)
I am testing this with my own credentials at present but eventually it would be replaced with a different used or a service account. I think everything is pointing at me entering them wrong somehow but I'm 2 days in and totally stumped.
Thanks all

Twilio Client c#

I have been using this code to send sms and voice calls for the last few days and now I am getting a weird response. On my recording status app I get the error: The remote name could not be resolved: 'api.twilio.com'. I have stripped down my function to a couple basic lines of code and still get the error.
TwilioClient.Init(_Twilio.ApiSid, _Twilio.ApiToken);
CallResource call = CallResource.Fetch(pathSid: sCallSid);
if (call != null)
{/*handle error*/}
The sCallSid string is valid and has a value. the ApiSid and ApiToken are working as well.
The 'api.twilio.com' resolved statement has stumped.
Any ideas how to isolate this issue and resolve it?

Sending multiple emails with transaction behavior (if one fails no emails get send)

Sorry if I failed to find an existing post with my problem.
What am I trying to do is as follows:
I simply want to send a couple of emails (2-3) to different people and more importantly with different content. The emails are official, important and also logged in the system. Long story short, I need when for some reason one of them fails to stop the sending of the others. I need either all of them sent or none of them.
What have I done so far
It is not the first time the system I worked on has to send an automatic email. The application is an ASP MVC website. So some time ago, I installed the MvcMailer (MvcMailer) and used it the way it was explained. It worked quite well and so I liked the idea of previewing the email (as you can give it a view to send).
So, in the light of my new problem I read carefully the MvcMailer documentation and did not find anything about sending multiple emails in transaction-like manner. Couldn't think of a way to force them to behave in this way. In my tests when I send an email, even if it is one email with a few CCs, all working mails get send and just one of them fails (wrong email name, closed port ... whatever).
I was hoping someone could suggest me a way to achieve something like this. I hope my explanation was sufficient and if not, let me know I will provide you with all details required.
If this is impossible with the MvcMailer, then is it possible with other tools ?
My implementation so far: (keep in mind I'm still in the testing stages)
public class MailerController : Controller
{
private MailerRepository mailerRep;
public MailerController()
{
this.mailerRep = new MailerRepository();
}
public void TestTransmittalEmail()
{
var model = this.mailerRep.GetTransmittalModel(1234); //I've stucked a specific clientId
var mailer = new TransmittalsMailer();
mailer.TransmittalEmail(model).Send();
}
}
public class TransmittalsMailer : MailerBase
{
public TransmittalsMailer()
{
}
public MvcMailMessage TransmittalEmail(TransmittalManifestModel model)
{
var mailMessage = new MvcMailMessage() { Subject = "Transmittals - TESTING EMAIL" };
//embedding a few images in the email
var resources = new Dictionary<string, string>();
resources["logo"] = PDFResourcePaths.VripackLogo;
resources["companyInfo"] = PDFResourcePaths.CompanyInfoBlock;
mailMessage.To.Add("test1#email.com");
mailMessage.Attachments.Add(new Attachment(#"D:\ASD\TransmittalFolders\1\Archives\150812.1433.MMA.rar"));
ViewData["model"] = model;
this.PopulateBody(mailMessage, "TransmittalEmailView", resources);
return mailMessage;
}
}
This is actually quite a difficult problem to solve. This is because when you send an email, it will work as long the SMTP server can be found (no exception will be thrown).
So you basically have to wait some arbitrary amount of time, and check the inbox of the email you sent it from for the delivery failure. If there is a delivery failure, you stop sending.
So long story short, you shouldn't probably do this. You should simply send all three and notify yourself some other way (probably another email) that there was a failure and which email failed.
You can check if an exception is thrown and then cancel the sending of subsequent emails until the issue is addressed. However this only deals with issues on your end on the sending of each email individual email. If the first two mails are successfully sent, and the last throws an exception, you can't unsend the first emails.
A possible workaround (ugly as it is) would be to initially send emails to an email address you control. If an exception is thrown at this step, log the error, and do not send any further emails until the error is dealt with. If no exceptions are raised, send the emails. However this does not and cannot handle issues that may occur on the recipients side.
A delivery failure notice is not guaranteed as depending on the configuration of your SMTP and the recipient SMTP a delivery failure notification might not be sent. Even if delivery failure notifications are enabled, the notification might not be sent for days (in one of my previous jobs, email delivery failure notifications were not sent until 14 days had elapsed).
Ideally, this should be dealt with at the initial input of the email addresses before you ever send any documents to anyone. You simply send a verification email to the email address in question and have the user clicking on a verification link back to a web service you control. Until this has been done, you don't send any emails to the intended recipient.

Bad Request error using SendGrid SMTP email submission

I am currently using C# in MVC4 ASP.NET 4.5.
My application creates a user and then submits a registration email to that use to authenticate their email. To create this script, I followed this tutorial: How to Send Email Using SendGrid with Windows Azure
I am using the latest version of SendGrid (2.1.1)
Code specific namespaces being used:
using SendGridMail;
using System.Net;
using System.Net.Mail;
Here is my code that creates the SendGrid mail message:
SendGrid message = SendGrid.GetInstance();
message.From = new MailAddress(fromEmail); //fromEmail is a MailAddress type
message.AddTo(userName); //this is a string
message.Html = body; //this is also a string
Then I go to send the email:
//Create login info for email
NetworkCredential credentials = new NetworkCredential("username", "password");
var trasnportSMTP = Web.GetInstance(credentials);
trasnportSMTP.Deliver(message); //smtp.sendgrid.net Send message
On the last line where "trasnportSMTP.Deliver(message);" I get this error:
Server Error in '/' Application.
Bad Request
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.Exception: Bad Request
The only things I've noticed while tracing is in the Exception Snapshot in View Details and the Output window.
I noticed that the:
TargetSite = {Void CheckForErrors(System.Net.Http.HttpResponseMessage)},
ReturnType is {Name = "Void" FullName = "System.Void"},
ReflectedType is {Name = "Web" FullName = "SendGridMail.Web"},
CustomAttributes - Count = 0,
Name = "CheckForErrors",
It's in the Module {SendGridMail.dll}.
Also in my output, there is the following:
"A first chance exception of type 'System.FormatException' occurred
in System.dll Step into: Stepping over non-user code
'SendGridMail.Web.Deliver'"
Can anyone give me some more insight on why this error is happening. I am using the correct username and password that is on the SendGrid account on Azure.
Thank you ahead of time.
Try adding the subject to your message
message.Subject = "this is the subject";
That should do it, I ran into the same problem when I was playing with this library.
This answer helped me (I'm using Azure):
SendGrid Tutorial resulting in Bad Request
In short: you need to get your credentials through azure (connect info at the marketplace), you might be using the wrong ones.
Digging through the documentation if you need to use the key which looks like something closer to "SG.asasfhiouce-JKIjjkvcdb_iouywerejkhwrnd" you should use the exact word "apikey" as the username and that key as the password
If it didn't work, try adding to the header "Authorization" with value "Bearer API_KEY_ABOVE_AS_WELL"
so it looks like
"Authorization" "Bearer SG.asasfhiouce-JKIjjkvcdb_iouywerejkhwrnd"

Paged LDap search fails with "The requested attribute does not exists"

I need to get the 'employeenumber' of all the employees whose 'epersonstatus=REMOVE' using an Ldap search implemented using .NET/C# like:
var connection = new LdapConnection("foo.bar.com:389");
connection.AuthType = AuthType.Anonymous;
connection.SessionOptions.ProtocolVersion = 3;
connection.Bind();
var request = new SearchRequest(
"dc=root,dc=com",
"(epersonstatus=REMOVE)",
SearchScope.Subtree,
new string[] { "employeenumber" });
Since there are thousands of entries I am using paged requests as proposed here:
http://dunnry.com/blog/PagingInSystemDirectoryServicesProtocols.aspx
I have also checked that the server supports paged requests as proposed here:
iPlanet LDAP and C# PageResultRequestControl
Once the flow reaches:
SearchResponse response = connection.SendRequest(request) as SearchResponse;
I get a DirectoryOperationException with message "The requested attribute does not exist".
By running the same query on a LDap client like softerra I get the entries (a thousand) and
the error.
Some help would be greatly appreciated.
I had a similar issue.
When using paged search, I got the exception "The server does not support the control. The control is critical.", when using non-paged search I received results (at least as long as the filter restricted the maximum number).
However I found out, that the error message is misleading - The problem was buried in the authentication.
Using AuthType.Basic (or AuthType.Anonymous) I received the error. Bus as soon as I switched to AuthType.Ntlm it worked.
Hope this helps...

Categories