Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
SolvedPrivate

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

Posted on 2014-12-19
7
Medium Priority
?
77 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 

Author Comment

by:StampIT
ID: 40510065
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
ID: 40512897
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
ID: 40513012
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:StampIT
ID: 40513363
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 2000 total points
ID: 40514510
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
ID: 40514909
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
ID: 40515170
The SMTP service was not running. So starting the SMTP service and using the correct port resolved the issue. Thanks.
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

719 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