Email- MS Exchange Server with BE.Net

Topics: ASP.NET 2.0
Jan 13, 2009 at 3:42 AM
Hi,
Can we use MS Exchange Server for emails?

If yes please let me know the way it can be achieved.

Thanks in Advance

Regards,
Rishi
Jan 27, 2009 at 10:40 PM
Edited Jan 27, 2009 at 10:41 PM
Would appreciate same info as well.  Normally other ASP.NET apps don't use credentials when sending to a LAN attached Exchange server, so the config option in BE settings seems to assume external ISP hosted SMTP connection.  We tried using the internal FQDN (server.domain.local) without specifying any credentials but the connection failed.

Note - I'm configuring 1.4.5.

Thanks,
Bill C.
Jan 28, 2009 at 1:41 AM
Couldn't you set your Exchange server to allow relaying from the server that's hosting BE, assuming that your BE isn't installed on your Exchange server? Or does there still have to be an authentication made? I can't remember off the top of my head right now. It's late and I'm tired. LOL

Feb 22, 2009 at 12:10 AM
Exchange is configured for relay.  Entered BE.NET 1.4.5 E-Mail settings with just SMTP FQDN and Port 25 without User/Pass and Test = Could Not Connect.  Here's what it looks like:

Coordinator
Feb 22, 2009 at 2:42 AM
You don't think you need a Username/Password?  You can make a small modification to the BE code to get more error information.  In the Settings.aspx.cs file under admin\Pages, you'll find this snippet of code:

} catch {
    lbSmtpStatus.Text = "Could not connect";
    lbSmtpStatus.Style.Add(HtmlTextWriterStyle.Color, "red");
}

Replace that with ...

} catch (Exception ex) {
    string errorMsg = "<br />" + ex.Message;
    while (ex.InnerException != null)
    {
        if (!string.IsNullOrEmpty(ex.InnerException.Message))
            errorMsg += "<br />" + ex.InnerException.Message;
        ex = ex.InnerException;
    }
    lbSmtpStatus.Text = "Could not connect - " + errorMsg;
    lbSmtpStatus.Style.Add(HtmlTextWriterStyle.Color, "red");
}

Then try 'Test mail settings'.  You should get some error information.
Feb 22, 2009 at 3:16 AM
Edited Feb 22, 2009 at 3:19 AM
If exchange was really configured for open relay (a very bad idea) or even for unauthenticated relaying from permitted hosts (not as bad but still not good), it will allow unauthenticated relaying provided you don't try to pass it user credentials.  you can test your relay settings by telnetting to your exchange server on port 25 from your webserver and sending a test message via telnet.

telent [server] 25
ehlo
mail from: blog@blogsite.com
rcpt to: youremailaddress@wherever.com
type some text
.
quit


it will tell you what it thinks of you connecting that way.
note you cannot type a backspace while doing that - you need to start over or type rset to reset connection
the "." is required to tell it its the end of the message.  

Note Exchange is commonly configured to require that the blogsite.com be a domain that is served by the exchange server so if the from address of blog@blogsite.com is not normally served by the exchange server, you will likely need to add an exception to allow it.

Feb 22, 2009 at 3:37 AM
Will try both of these and advise of results.  BTW, Exchange is configured to accept SMTP from specific server Private IP, not open Relay.  The note about the Exchange server needing to accept the blog domain email (in my example @blogsite.com)... is the likely culprit I'm going to check first.  Then the verbose error code to further troubleshoot.  Thanks for the posts!
Feb 28, 2009 at 4:17 PM
To test blog 1.4.5 - I configure the email settings with my email id and my windows user name and password. I am getting emails but how can other BE ('editors') users get these alerts?
I was not able to get much from the discussions above.
Coordinator
Feb 28, 2009 at 5:54 PM
In which situations do you want other editors to get emails?  And which editors should be sent the emails?

The BE code currently sends an email to the email address you have setup on the Settings tab in the control panel.  You could modify the BE code so it also sends emails to the editors.  There's three or so instances in the BE code where an email is sent out.  The places that would need to be modified would depend on which situations you want additional emails to be sent.  And the modifications needed to make to each of these places depends on which other editors you would want to be sent emails (e.g. some of the editors, or all the editors).
Mar 4, 2009 at 7:14 AM
I have the following requirements:-
1. All 'Editors' should recive an email on comments, added to all of their post.
2. All 'Editors' should get an email when a new post is posted to the blog.

How and Where can I modify the code to achieve the above result.

Thanks,
Rishi

On 28 Feb 2009 10:54:30 -0800, BenAmada <notifications@codeplex.com> wrote:
> From: BenAmada
>
> In which situations do you want other editors to get emails? And which
> editors should be sent the emails?
>
> The BE code currently sends an email to the email address you have setup on
> the Settings tab in the control panel. You could modify the BE code so it
> also sends emails to the editors. There's three or so instances in the BE
> code where an email is sent out. The places that would need to be modified
> would depend on which situations you want additional emails to be sent. And
> the modifications needed to make to each of these places depends on which
> other editors you would want to be sent emails (e.g. some of the editors, or
> all the editors).
>
>


--
Rishi Girdhar
Coordinator
Mar 4, 2009 at 6:30 PM
Rishi: There's a couple of ways to do this.  What I'd suggest is making sure you have an email address setup for each of the editors on the Profiles tab in the control panel.  Then the code can iterate through all the profiles checking for editors/admins that have an email address in their profile.

It looks like you're using BE 1.4.5 which has problems with the Profiles page when you have multiple editors.  I'd recommend replacing your Profile.aspx and Profile.aspx.cs files in the admin\Pages folder with the latest versions of these two files.  You can either download the latest version on the Source Code tab above, or you can copy-and-paste the latest contents of these files into your existing files.  Latest content for Profiles.aspx is here and latest content for Profiles.aspx.cs is here.

For # 1, to send an email to all editors when a comment is left ... in the SendCommentMail.cs file in the App_Code\Extensions folder, you'll see this line of code:

mail.To.Add(BlogSettings.Instance.Email);

Replace that line of code with what's below:

foreach (AuthorProfile profile in AuthorProfile.Profiles)
{
    if (!string.IsNullOrEmpty(profile.EmailAddress))
    {
        mail.To.Add(profile.EmailAddress);
    }
}

That will include all editors in the comment notification email.

For # 2, to send an email when a new post is posted to the blog ... I don't think there's any code in BE that currently sends out an email to the author or editor when a new post is created.  But, this functionality can be achieved with an Extension.  I just created an extension that would do this.

In the App_Code\Extensions folder, create a new file named "NewPostNotificationEmailsToEditors.cs".  This extension will send an email to all the editors whenever a post is first published.  The content for this file is below:

#region using

using System;
using System.Web;
using BlogEngine.Core.Web.Controls;
using BlogEngine.Core;
using System.Net.Mail;
using System.Threading;

#endregion

/// <summary>
/// Emails all editors when a new post is saved and marked as published or when an existing
/// post is saved and is going from an unpublished state to a published state.
/// </summary>
[Extension("Emails all editors when a post is first published.", "1.0", "Ben Amada")]
public class NewPostNotificationEmailsToEditors
{
    /// <summary>
    /// Hooks up an event handler to the Post.Saved event.
    /// </summary>
    static NewPostNotificationEmailsToEditors()
    {
        Post.Saved += new EventHandler<SavedEventArgs>(Post_Saved);
        Post.Saving += new EventHandler<SavedEventArgs>(Post_Saving);
    }

    private static string _SendNewPostEmailsContextItemKey = "NewPostNotificationEmailsToEditors";

    private static bool SendNewPostEmails
    {
        get
        {
            if (HttpContext.Current.Items.Contains(_SendNewPostEmailsContextItemKey))
                return (bool)HttpContext.Current.Items[_SendNewPostEmailsContextItemKey];
            else
                return false;
        }
        set
        {
            if (HttpContext.Current.Items.Contains(_SendNewPostEmailsContextItemKey))
                HttpContext.Current.Items[_SendNewPostEmailsContextItemKey] = value;
            else
                HttpContext.Current.Items.Add(_SendNewPostEmailsContextItemKey, value);
        }
    }

    static void Post_Saving(object sender, SavedEventArgs e)
    {
        // initialize to false
        SendNewPostEmails = false;

        Post post = (Post)sender;
        if (e.Action == SaveAction.Insert && post.IsPublished)
            SendNewPostEmails = true;
        else if (e.Action == SaveAction.Update && post.IsPublished)
        {
            Post preUpdatePost = BlogEngine.Core.Providers.BlogService.SelectPost(post.Id);
            if (!preUpdatePost.IsPublished)
                SendNewPostEmails = true;
        }
    }

    static void Post_Saved(object sender, SavedEventArgs e)
    {
        if (SendNewPostEmails)
        {
            Post post = (Post)sender;
            foreach (AuthorProfile profile in AuthorProfile.Profiles)
            {
                if (!string.IsNullOrEmpty(profile.EmailAddress))
                {
                    MailMessage mail = CreateEmail(post);
                    mail.To.Add(profile.EmailAddress);
                    Utils.SendMailMessageAsync(mail);
                }
            }
        }
    }

    private static MailMessage CreateEmail(Post post)
    {
        MailMessage mail = new MailMessage();
        mail.Subject = post.Title;
        mail.Body = "There'a new post available.<br /> <a href=\"" + post.AbsoluteLink + "\">" + post.Title + "</a><br />";
        mail.Body += post.Description;
        mail.From = new MailAddress(BlogSettings.Instance.Email, BlogSettings.Instance.Name);
        return mail;
    }

}