SolvedPrivate

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

Posted on 2014-12-19
7
70 Views
Last Modified: 2016-02-15
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
Comment
Question by:StampIT
  • 5
  • 2
7 Comments
 

Author Comment

by:StampIT
Comment Utility
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
 
LVL 7

Expert Comment

by:Utkarsh Kulkarni
Comment Utility
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
 

Author Comment

by:StampIT
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:StampIT
Comment Utility
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
 
LVL 7

Accepted Solution

by:
Utkarsh Kulkarni earned 500 total points
Comment Utility
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
 

Author Comment

by:StampIT
Comment Utility
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
 

Author Closing Comment

by:StampIT
Comment Utility
The SMTP service was not running. So starting the SMTP service and using the correct port resolved the issue. Thanks.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 5 109
Long URL in SMS 6 22
Sql query for filter 12 21
Usage Scenarios for Extended Events? 1 9
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
My client sends data in an Excel file to me to load them into Staging database. The file contains many sheets that they have same structure. In this article, I would like to share the simple way to load data of multiple sheets by using SSIS.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now