Send email from ASP.NET application through an external exchange server - asp.net

I have an ASP.NET application pointing the 2.0 version of the framework written in C# deployed in IIS6.
I am maintaining the application therefore some of its parts are not clear to me.
The application has to send email messages through an external exchange server to and from oldMailAddress and has been done correctly. Now I added a newMailAddress and I haven't changed anything else from the code.
The oldMailAddress receives the emails send through the application, the newMailAddress doesn't. I tried to send emails to newMailAddress from my mailbox and it works correctly.
I looked for solutions on the Internet and I saw that most of them are based on SMPT services installed in IIS6. Well, I do not have these services installed and I think it is not necessary since the application was working with oldMailAddress. Hereby I attach the code that I use, I hope anybody might help.
MailMessage oMail = new MailMessage();
switch (destination)
{
case "production":
strMailAddress = "newMailAdress#domain.eu";
oMail.From = new MailAddress(strMailAddress);
oMail.To.Add(strMailAddress);
break;
case "warehouse":
strMailAddress = "oldMailAdress#domain.eu";
oMail.From = new MailAddress(strMailAddress);
oMail.To.Add(strMailAddress);
break;
}
oMail.Subject = sTitle;
oMail.IsBodyHtml = true;
oMail.Body = sHTML;
SmtpClient oSmpt = new SmtpClient("external.smpt.eu");
oSmpt.Send(oMail);

My guess is that there is either an auth issue, or the email is being caught in a spam folder # newMailAddress.
If you still can't figure it out, then I would recommend enabling logging (if you have write permissions). I have an explanation of how to enable logging over at:
http://www.systemnetmail.com/faq/4.10.aspx

Related

Apple Push Notifications on Windows Server 2008 not working

I'm stuck and I hope somebody here will help me.
For a few days I'm trying to send apple push notifications from windows server 2008. But nothing is send or received. In fact, I've moved everything to simple mvc application so it can be debug easier and I've noticed that it hangs when I try to force sending the push notifications (on push.StopAllServices(true); )
They were working (and still are) from windows server 2012R2 with the same approach to send notifications so I guess the certificates and code are fine.
I'm using windows service & push sharp to send notifications. target framework: 4
The test project is MVC 2.0 target framework 4
I've installed asp.net 4.5 on the server
I've installed certificate via mmc in Personal (the private key is available, granted permission for IUSR and IIS_USRS) and Trusted Root Certificate Authorities.
Port 2195 is open
Here's the code:
private void Initizalize()
{
push = new PushBroker();
//appleCert is path to my .p12 certificate on disk.
var cert = new ApplePushChannelSettings(false, appleCert, "***");
//I've read somewhere that it was helpful to force send notifications. Well, in my case it's not.
var settings = new PushServiceSettings();
settings.AutoScaleChannels = false;
settings.Channels = 3;
settings.MaxAutoScaleChannels = 3;
//Wire up the events for all the services that the broker registers
push.OnNotificationSent += NotificationSent;
push.OnChannelException += ChannelException;
push.OnServiceException += ServiceException;
push.OnNotificationFailed += NotificationFailed;
push.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired;
push.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged;
push.OnChannelCreated += ChannelCreated;
push.OnChannelDestroyed += ChannelDestroyed;
push.RegisterAppleService(cert, settings);
}
public void SendPush()
{
Initizalize();
var token = hard coded token that exists;
var output = "msg";
push.QueueNotification(new AppleNotification()
.ForDeviceToken(token)
.WithAlert(output)
.WithBadge(1)
.WithSound("default"));
push.StopAllServices(true);
}
It raises event "OnNotificationSent", nothing else.
One more thing that may be important. Sending and receiving push notifications was and is working from windows server 2012 but it's windows azure virtual machine so installing the certificate was different.
Any ideas?
Even the information that it cannot be done will be helpful!
edit:
One problem solved:
Apparently you need to have reference to NewtonSoft.Json in order to call StopAllServices. It doesn't hang now but nothing is received by the device.
Looks like on Windows Server 2008 apns certificates private key must have granted permission for user that wants to use it.
As I wrote before I've granted it for IIS_USRS but that's only for applications running under IIS. And since I was using windows service it wasn't working.
So what I did was check user for windows service (Services -> Log On As) and granted permission to certificates private key for this user.

Not able send Email using coltware lib in Flex

We are working on Flex technology to develop a desktop application, in which we will send mail to specified users.
We have used coltware library to send email to users. But the system is not sending email. We have checked code and there system is not showing any error in this.
My code is:
sender = new SMTPSender();
sender.setParameter(SMTPSender.HOST,"smtp.mandrillapp.com");
sender.setParameter(SMTPSender.PORT,587);
sender.setParameter(SMTPSender.AUTH,true);
sender.setParameter(SMTPSender.USERNAME,"myusername");
sender.setParameter(SMTPSender.PASSWORD,"mypassword");
message = new MimeMessage();
from = new INetAddress("sendfromEmailID", "From Label");
message.setFrom(from);
toRecpt = new INetAddress("sentToEmailID", "To Label");
message.addRcpt(RecipientType.TO,toRecpt);
message.setSubject("Email from Test Application");
message.setTextBody("Hello User");
sender.send(message);
sender.close();
Can you please tell us? Is anything worng with above code. if not, then why the system is not sending email??
Waiting for solution.
Does Mandrill requires two-step authentication? (OAuth, OpenAPI)?
Also check if it needs tlsocket to be specified
var tlssocket:TLSSocket = new TLSSocket();
sender.setParameter(SMTPSender.SOCKET_OBJECT, tlssocket);

Novell eDirectory with .NET DirectoryServices

In our company, we have a project which should use Novell eDirectory with .net applications.
I have tried Novell Api (http://www.novell.com/coolsolutions/feature/11204.html) to connect between .NET applications. It is working fine.
But, as per requirement, we specifically need .net API to connect not with Novell Api, which is not working. Connection and binding with .NET Api DirectoryServices not working.
Our Novell eDirectory is installed with following credentials:
IP address: 10.0.x.xx(witsxxx.companyname.com)
Tree : SXXXX
New Tree Context: WIxxxK01-NDS.OU=STATE.O=ORG
ADMIN Context is: ou=STATE,o=ORG
admin : admin
password: admin
I used Novell Api and used following code
String ldapHost ="10.0.x.xx";
String loginDN = "cn=admin,cn=WIxxxK01-NDS,OU=STATE,o=ORG";
String password = string.Empty;
String searchBase = "o=ORG";
String searchFilter = "(objectclass=*)";
Novell.Directory.Ldap.LdapConnection lc = new Novell.Directory.Ldap.LdapConnection();
try
{
// connect to the server
lc.Connect(ldapHost, LdapPort);
// bind to the server
lc.Bind(LdapVersion, loginDN, password);
}
This is binding correctly and searching can be done.
Now my issue is with when I trying to use .NET APi and to use System.DirectoryServices
or System.DirectoryServices.Protocols, it is not connecting or binding.
I can't even test the following DirectoryEntry.Exists method. It is going to exception.
string myADSPath = "LDAP://10.0.x.xx:636/OU=STATE,O=ORG";
// Determine whether the given path is correct for the DirectoryEntry.
if (DirectoryEntry.Exists(myADSPath))
{
Console.WriteLine("The path {0} is valid",myADSPath);
}
else
{
Console.WriteLine("The path {0} is invalid",myADSPath);
}
It is saying Server is not operational or Local error occurred etc. I don't know what is happening with directory path.
I tried
DirectoryEntry de = new DirectoryEntry("LDAP://10.0.x.xx:636/O=ORG,DC=witsxxx,DC=companyname,DC=com", "cn=admin,cn=WIxxxK01-NDS,o=ORG", "admin");
DirectorySearcher ds = new DirectorySearcher(de, "&(objectClass=user)");
var test = ds.FindAll();
All are going to exceptions.
Could you please help me to solve this? How should be the userDN for DirectoryEntry?
I used System.DirectoryServices.Protocols.LdapConnection too with LdapDirectoryIdentifier and System.Net.NetworkCredential but no result. Only same exceptions.
I appreciate your valuable time and help.
Thanks,
Binu
To diagnose your LDAP connection error, get access to the eDirectory server from the admins, and use iMonitor (serverIP:8028/nds and select Dstrace), in Dstrace clear all tabs and enable LDAP tracing, then do your bind see what happens on the LDAP side to see if there is a more descriptive error there. Or if you even get far enough to bind and make a connection.

How to pass HostingEnvironment.Impersonate credentials to ExchangeService EWS?

Is it possible to pass the credentials of the user browsing my asp.net web application to the EWS FindAppointments call?
I'm only trying to return calendar details for the active browsing user, who will without doubt have permission to read their own calendar, so the issue should not relate to Exchange impersonation with the EWS api discussed here.
The code below works just fine when running localhost, but running from the web server, despite Windows Authentication and Identity Impersonation being configured it throws an access denied error.
using (HostingEnvironment.Impersonate())
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.UseDefaultCredentials = true;
service.AutodiscoverUrl(UserEmailAddress);
Mailbox mb = new Mailbox(UserEmailAddress);
FolderId cfCalendarFolderID = new FolderId(WellKnownFolderName.Calendar, mb);
CalendarView cvCalendarView = new CalendarView(DateTime.Now, DateTime.Now.AddDays(30), 1000);
cvCalendarView.MaxItemsReturned = 3;
Perhaps I'm missing a simple way to pass the HostingEnvironment credentials to my ExchangeService object?
Is there a way to check what the service.UseDefaultCredentials are?
I'm not able to use the following as there isn't a way to get the password from the windows authenticated impersonated user.
service.Credentials = new System.Net.NetworkCredential(username, password, domain);
I've also tried the following, but get the same ServiceResponseException access denied errot.
service.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
service.PreAuthenticate = true;
Thanks in advance of your kind assistance.
Additional info which may or may not be relevant:
The Application Pool Identity for the website is NetworkService.
The UserEmailAddress variable is set from an AD lookup based on System.Security.Principal.WindowsIdentity.GetCurrent().Name
EDIT (14th Aug 2012)
To achieve what I'd like to do above, I believe the HostingEnvironment.Impersonate isn't required.
Instead I need to use the ExchangeService's ImpersonatedUserId property.
More details on that here
Only problem though is we're running Exchange 2007 and the power shell command for enabling a service account to impersonate all users (that you would use pass in to the .Credentials parameter) only appears to be compatible with Exchange 2010.
You should try using WebCredentials instead of NetworkCredential - see related SO post. There seems to be an issue with EWS and AutoDiscover + NetworkCredentials

Unable to send emails to external domain using SMTP

I am not able to send emails to external domain addresses like 'user.one#asdf.com' using the code below.
SmtpClient smtpClient = new SmtpClient(smtpMailServer);
smtpClient.UseDefaultCredentials = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
//Sending mail.
smtpClient.Send(mailMessage);
I get an exception -
Mailbox unavailable. The server response was: 5.7.1 Unable to relay for xxx#example.com
If I change the DeliveryMethod to -
smtpClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
I am able to send the emails on my local machine. But it fails on the production site with an exception -
Cannot get IIS pickup directory
Can you please suggest me what to do?
I had this issue and authenticating fixed it see below:
SmtpClient client = new SmtpClient(EmailServer, 25);
var SmtpUser = new System.Net.NetworkCredential("domain\\username", "password");
client.Credentials = SmtpUser;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
I had to use the double slash since one slash is the escape character so use two for it to work.
If you were to look up the MX record for the destination address (in your example, it is asdf.com) and then use that for the host property of SmtpClient, it should accept the message for delivery without authentication since it's to a local user. This is not easy to do since System.Net doesn't provide a managed DNS class that can return MX records but you can P/invoke unmanaged code to do it. Otherwise you will need to be sure that whatever SMTP server you are connecting to will relay for you and then set the Credentials property of SmtpClient to the appropriate credentials for connecting to that server. Setting the DeliveryMethod to PickupDirectoryFromIIS still only writes a file to the IIS pickup directory so it's only writing a file, it isn't doing an actual send.
You usually need to authenticate with the external mail server using a username/password. As you are using an external server this will not know the credentials you are passing. This may be your issue.
I faced this issue, which I solved by added a domain with "*.com" as domain name and type as "remote", under IIS 6.0 Manager/SMTP Virtual Server/Domains.
Although in my case the SMTP server allowed anonymous access.

Resources