Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Private
  • Views: 78
  • Last Modified:

SSIS email With Script Task and C#(System.Net.Mail)

I am attempting to send an email using the code below in a script task. After execution the package eventually errors out. The message. “System.Net.Mail.SmtpException: The operation timed out”. Is it something in the code or is there another issue. I have also tried with VB but get the same result. Thanks.

/*
   Microsoft SQL Server Integration Services Script Task
   Write scripts using Microsoft Visual C# 2008.
   The ScriptMain is the entry point class of the script.
*/

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;

namespace ST_b3ce18a40f624932943eb6d8a30c09e8.csproj
{
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {

        #region VSTA generated code
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion

        /*
            The execution engine calls this method when the task executes.
            To access the object model, use the Dts property. Connections, variables, events,
            and logging features are available as members of the Dts property as shown in the following examples.

            To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value;
            To post a log entry, call Dts.Log("This is my log text", 999, null);
            To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, true);

            To use the connections collection use something like the following:
            ConnectionManager cm = Dts.Connections.Add("OLEDB");
            cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;";

            Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
            
            To open Help, press F1.
      */

        public void Main()
        {
            var theMailer = new MailMessage();
            theMailer.To.Add("myname@mydomain.com");
            theMailer.From = new MailAddress("postfixemail@mydomain.com", "My Name");
            theMailer.IsBodyHtml = true;
            theMailer.Subject = "SSIS Email";
            theMailer.Body = "See Attachment";
            //*theMailer.Attachments.Add(new Attachment("C:\\LearnItFirstTest\\OurReport.xls"));
            var myClient = new SmtpClient("smtp.gmail.com", 465);
            myClient.EnableSsl = true;
            myClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            myClient.UseDefaultCredentials = false;
            myClient.Credentials = new NetworkCredential("postfixemail@mydomain.com", "MyPassword");

            myClient.Send(theMailer);

            Dts.TaskResult = (int)ScriptResults.Success;
        }
    }
}
0
StampIT
Asked:
StampIT
  • 5
  • 2
1 Solution
 
StampITAuthor Commented:
Additional Information. The Server the package is running on did not have Internet Information Services Active. Specifically the SMTP Server feature. This service has been started but there has been no difference in performance of the package. Not sure if the Server has to be rebooted to complete the process ??
0
 
Utkarsh KulkarniCommented:
Can you try to use default Mail sending Task in SSIS?
If not, you can check http://www.mssqltips.com/sqlservertip/1731/sending-email-from-sql-server-integration-services-ssis/ for help.
0
 
StampITAuthor Commented:
The Send Mail Task does not allow the use of user names and passwords required for gmail. I will check out the link. Thanks.
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
StampITAuthor Commented:
Although I did not completely understand all that was done in Part 2 of the link I believe I am doing the same thing in the code I posted except I'm using C# and the mail settings are hard coded rather than from a SQL table. I think my issue may have more to do with the SMTP setup on the server than on the code. Any help is appreciated. Thanks.
0
 
Utkarsh KulkarniCommented:
Just make sure of following
1. SMTP Service is installed & running
2. The server that executes this code has been allowed to use internet & SMTP.
3. Check for Firewall / Antivirus setting.
0
 
StampITAuthor Commented:
1. Task Manager confirms that the SMTP Service is running
2. The server can get to the internet so am presuming it can use SMTP
3. No firewall or antivirus setting that would prevent internet access.

I get the error message below when I attempt to execute the package

Error: 0x1 at Step 2 - Send Mail: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Net.Mail.SmtpException: The operation has timed out.
   at System.Net.Mail.SmtpClient.Send(MailMessage message)

Is there a way to test SMTP outside of SSIS and SQL Server that may better isolate the issue ?
0
 
StampITAuthor Commented:
The SMTP service was not running. So starting the SMTP service and using the correct port resolved the issue. Thanks.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now