locked
Send mail - Imports System.Net.Mail RRS feed

  • Question

  • I created a class under Client>SendMail.vb in order to be able to send an email to a client. I have managed to create an "automated email using that guide http://blogs.msdn.com/b/bethmassi/archive/2011/01/27/how-to-send-html-email-from-a-lightswitch-application.aspx"

    I have problem importing System.Net.Mail

    Can you help with that?

    ========

    Imports System.Net
    Imports System.Net.Mail


    Public Class Stile
        Const SMTPServer As String = "smtpserver"
        Const SMTPUserId As String = "emailid"
        Const SMTPPassword As String = "password"
        Const SMTPPort As Integer = 25

        Public Shared Sub SendMail(ByVal sendFrom As String,
                                   ByVal sendTo As String,
                                   ByVal subject As String,
                                   ByVal body As String)

            Dim fromAddress = New MailAddress(sendFrom)
            Dim toAddress = New MailAddress(sendTo)
            Dim mail As New MailMessage

            With mail
                .From = fromAddress
                .To.Add(toAddress)
                .Subject = subject

                If body.ToLower.Contains("<html>") Then
                    .IsBodyHtml = True
                End If

                .Body = body
            End With

            Dim smtp As New SmtpClient(SMTPServer, SMTPPort)
            smtp.Credentials = New NetworkCredential(SMTPUserId, SMTPPassword)
            smtp.Send(mail)
        End Sub
    End Class

    =====

    Monday, March 7, 2011 2:55 PM

Answers

  • If you're writing client side code, Silverlight doesn't support the System.Net.Mail namespace which probably explains the problem.

    Monday, March 7, 2011 4:37 PM

All replies

  • If you're writing client side code, Silverlight doesn't support the System.Net.Mail namespace which probably explains the problem.

    Monday, March 7, 2011 4:37 PM
  • Yes,I am writting client side code for a desktop app. What method can we use in order to send email from a desktop app? Thank you

    Tuesday, March 8, 2011 9:26 AM
  • If you have to do this client side, the only other method that springs to mind is to automate Outlook. Beth has some example code on her blog which demonstrates this.

     

    Tuesday, March 8, 2011 10:13 AM
  • I have already seen the blog which describes  outlook mail functions. It would be better if we could send it without using outlook. Does anyone have idea how could we send email direct from a desktop app using Lightswitch?

    Thanks

    Tuesday, March 8, 2011 10:28 AM
  • You have SQL Server can't you use the SSIS stack? There is a send mail task that is very simple to use.
    mysorian
    Tuesday, March 8, 2011 11:52 AM
  • Yes,it is build on SQL Server.Can you provide a working sample on that?

    Thank you

    Tuesday, March 8, 2011 2:16 PM
  • Import System.Net. System.Net.Mail is namespace within that assembly. See here for an example of sending an email from LightSwitch... http://www.paulspatterson.com/technology/lightswitch/microsoft-lightswitch-send-an-email-from-lightswitch/ Cheers!
    PP
    Friday, April 8, 2011 2:38 PM
  • Paul Patterson has an updated tutorial that shows using it from a web deployed LightSwitch application:

    http://www.paulspatterson.com/technology/lightswitch/microsoft-lightswitch-sending-emails-from-the-client/#more-2896

    Here is the code in C#:

    // Adapted from:
    // http://www.paulspatterson.com/technology/lightswitch/
    // microsoft-lightswitch-sending-emails-from-the-client/#more-2896
    
    using System.Net;
    using System.Net.Mail;
    using System.Configuration;
    using System;
    using LightSwitchApplication.Properties;
    
    namespace LightSwitchApplication
    {
      public class MailHelper
      {
        private string _SMTPServer { get; set; }
        private string _SMTPUserId { get; set; }
        private string _SMTPPassword { get; set; }
        private int _SMTPPort { get; set; }
        private bool _SMTPSSL { get; set; }
    
        private string _MailFrom { get; set; }
        private string _MailFromName { get; set; }
        private string _MailTo { get; set; }
        private string _MailToName { get; set; }
        private string _MailSubject { get; set; }
        private string _MailBody { get; set; }
    
        public MailHelper(string SendFrom, 
          string SendFromName, string SendTo, 
          string SendToName, string Subject, 
          string Body)
        {
          _MailFrom = SendFrom;
          _MailFromName = SendFromName;
          _MailTo = SendTo;
          _MailToName = SendToName;
          _MailSubject = Subject;
          _MailBody = Body;
    
          _SMTPServer = Settings.Default.SMTPServer;
          _SMTPUserId = Settings.Default.SMTPUserID;
          _SMTPPassword = Settings.Default.SMTPPassword;
          _SMTPPort = Settings.Default.SMTPPort;
          _SMTPSSL = Settings.Default.SMTPSSL;
        }
    
        public void SendMail()
        {
          MailMessage mail = new MailMessage();
    
          System.Net.Mail.MailAddress mailFrom = 
            new System.Net.Mail.MailAddress(_MailFrom, _MailFromName);
    
          System.Net.Mail.MailAddress mailTo = 
            new System.Net.Mail.MailAddress(_MailTo, _MailToName);
    
          var _with1 = mail;
          _with1.From = mailFrom;
          _with1.To.Add(mailTo);
          _with1.Subject = _MailSubject;
          _with1.Body = _MailBody;
    
          SmtpClient smtp = new SmtpClient(_SMTPServer, _SMTPPort);
          smtp.EnableSsl = _SMTPSSL;
    
          smtp.Credentials = 
            new NetworkCredential(_SMTPUserId, _SMTPPassword);
    
          smtp.Send(mail);
        }
      }
    }
    


    http://www.adefwebserver.com

    http://LightSwitchHelpWebsite.com

    Friday, May 27, 2011 1:03 PM
  • Hi Michael

    This updated tutorial  of Paul Patterson also uses System.Net.Mail, which leads to the same starting point. That is still not acknowledged. And the procedure to go through the Outlook adds a step in this procedure is not required.


    I think it would be nice Silverlight Give the System.Net.Mail namespace support.

     

    Jaime

     

     

    Thursday, June 2, 2011 11:32 AM
  • As smtp seems ok for you.  Use a table as the email queue.  Create an OutBox Table.  Then handle the Inserted event at the server side.  As your at the server, you have access to Net.Mail.  Create a SendMail screen using the outbox table.   Personally I like this better local COM automation as automation can be brittle sometimes for unknown reasons and will not work anyway if you change to web UI. Sio this solution will work in either desktop or web client.  Also you get a history and failure/retry semantics you can build in.   

    Thursday, June 2, 2011 4:25 PM