私は新しいASP.NET開発者であり、簡単な登録システムを開発しています。現在、システム管理者がイベントの開始日より前に登録ユーザーにリマインダーを送信できるようにしようとしています。利用可能なすべてのイベントは、そのイベントの登録ユーザーにリマインダーを送信するためのボタンが付いたGridViewに一覧表示されます。参考までに、私は次のデータベース設計を持っています。
Employees Table: NetID, Name
Events Table: ID, Title
BookingDetails Table: BookingID, EventID, NetID
** NetIDは従業員のユーザー名であることに注意してください
ASP.NETコード:
<asp:GridView ID="ListOfAvailableEvents_GrivView" runat="server"
AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333"
GridLines="None" AllowPaging="True">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" CssClass="generaltext" />
<Columns>
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" />
<asp:BoundField DataField="StartDateTime" HeaderText="Start Date & Time" SortExpression="StartDateTime" />
<asp:BoundField DataField="EndDateTime" HeaderText="End Date & Time" SortExpression="EndDateTime" />
<asp:BoundField DataField="NumberOfSeats" HeaderText="Number of Seats" SortExpression="NumberOfSeats" />
<asp:BoundField DataField="Number of Bookings" HeaderText="Number of Bookings" ReadOnly="True"
SortExpression="Number of Bookings" />
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="sendButton" runat="server" CssClass="button" Text="Send Reminder →"
OnClick="btnSend_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle Font-Bold="True" CssClass="complete" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PM_RegistrationSysDBConnectionString %>"
SelectCommand="SELECT Events.Title, Events.Description, Events.Location, Events.StartDateTime, Events.EndDateTime, Events.NumberOfSeats, COUNT(BookingDetails.BookingID) AS [Number of Bookings] FROM Events INNER JOIN BookingDetails ON Events.ID = BookingDetails.EventID WHERE (Events.IsActive = 1) GROUP BY Events.Title, Events.Description, Events.Location, Events.StartDateTime, Events.EndDateTime, Events.NumberOfSeats ORDER BY Events.StartDateTime DESC"></asp:SqlDataSource>
C#コード:
protected void btnSend_Click(object sender, EventArgs e)
{
GridViewRow gvrow = (GridViewRow)(((Button)sender)).NamingContainer;
HiddenField1.Value = ListOfAvailableEvents_GrivView.DataKeys[gvrow.RowIndex].Value.ToString();
string title = gvrow.Cells[0].Text;
string description = gvrow.Cells[1].Text;
string location = gvrow.Cells[2].Text;
string startDateTime = gvrow.Cells[3].Text;
string endDateTime = gvrow.Cells[4].Text;
string connString = ".............................;";
using (SqlConnection conn = new SqlConnection(connString))
{
var sbEmailAddresses = new System.Text.StringBuilder(2000);
int eventID = Convert.ToInt32(HiddenField1.Value);
//Open DB connection
conn.Open();
string cmdText = @"SELECT dbo.Events.Title, dbo.Employees.NetID
FROM dbo.BookingDetails INNER JOIN
dbo.Events ON dbo.BookingDetails.EventID = @EventID INNER JOIN
dbo.Employees ON dbo.BookingDetails.NetID = dbo.Employees.NetID";
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
cmd.Parameters.AddWithValue("@EventID", eventID);
cmd.ExecuteNonQuery();
SqlDataReader reader = cmd.ExecuteReader();
if (reader != null)
{
while (reader.Read())
{
var sName = reader.GetString(0);
if (!string.IsNullOrEmpty(sName))
{
if (sbEmailAddresses.Length != 0)
{
sbEmailAddresses.Append(", ");
}
sbEmailAddresses.Append(sName).Append("@appServer.com");
}
}
}
reader.Close();
var sEMailAddresses = sbEmailAddresses.ToString();
string body = "..........................";
int sendCount = 0;
List<string> addressList = new List<string>(sEMailAddresses.Split(','));
StringBuilder addressesToSend = new StringBuilder();
for (int userIndex = 0; userIndex < addressList.Count; userIndex++)
{
sendCount++;
if (addressesToSend.Length > 0)
addressesToSend.Append(",");
addressesToSend.Append(addressList[userIndex]);
if (sendCount == 10 || userIndex == addressList.Count - 1)
{
SendEmail(addressesToSend.ToString(), "", "Registration REMINDER Notification", body, true);
addressesToSend.Clear();
sendCount = 0;
}
}
cmd.ExecuteNonQuery();
}
//reset the value of hiddenfield
HiddenField1.Value = "-1";
}
}
protected void SendEmail(string toAddresses, string fromAddress, string MailSubject, string MessageBody, bool isBodyHtml)
{
SmtpClient sc = new SmtpClient("MAIL ADDRESS");
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("app@appServer.com", "Registration System");
// In case the mail system doesn't like no to recipients. This could be removed
//msg.To.Add("app@appServer.com");
msg.Bcc.Add(toAddresses);
msg.Subject = MailSubject;
msg.Body = MessageBody;
msg.IsBodyHtml = isBodyHtml;
sc.Send(msg);
}
catch (Exception ex)
{
throw ex;
// something bad happened
//Response.Write("Something bad happened!");
}
}
しかし、私はメールを送ることができませんでした。理由を教えてください。
アップデート:
コードを作成しましたが、実行してもエラーは発生しませんでした。デバッグしようとしましたが、デバッグを行ったところ、SendEmail(string toAddresses, string fromAddress, string MailSubject, string MessageBody, bool isBodyHtml)
メソッドの呼び出しが機能していないことがわかり、理由がわかりません。
では、選択したイベントの登録ユーザーにメールを送信する方法を教えてください。